Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d6fab5634 | |||
| 9c638fc3e5 | |||
| 43f6fb03fe | |||
| ac83907e7c | |||
| 3cd267ee6e | |||
| e1b756ac99 | |||
| 628cddc3ae | |||
| cf6a768f6b | |||
| a62778a030 | |||
| 6b491a9a41 |
@@ -0,0 +1,4 @@
|
|||||||
|
cd ./frontend
|
||||||
|
npm run dev
|
||||||
|
cd ..
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
# backend/api/router.py
|
|
||||||
|
|
||||||
from fastapi import APIRouter
|
|
||||||
from backend.api.v1.endpoints import ping, text_manager_endpoint
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
router.include_router(ping.router, prefix="/api/v1/ping")
|
|
||||||
router.include_router(text_manager_endpoint.router, prefix="/api/v1/text_manager")
|
|
||||||
@@ -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"
|
||||||
|
}]
|
||||||
|
}
|
||||||
+6
-2
@@ -1,13 +1,17 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from fastapi import Path
|
from fastapi import Path
|
||||||
|
|
||||||
from backend.schemas.text_manager_schema import BibliotecaInput, NotaInput
|
from backend.domains.text_manager.text_manager_schema import BibliotecaInput, NotaInput
|
||||||
|
|
||||||
from fastapi.concurrency import run_in_threadpool
|
from fastapi.concurrency import run_in_threadpool
|
||||||
from backend.db.conexion import get_conexion
|
from backend.db.conexion import get_conexion
|
||||||
from backend.services.text_manager_srvc import *
|
from backend.domains.text_manager.text_manager_srvc import *
|
||||||
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
||||||
|
|
||||||
|
from entrypoint.init_db import db_credencial
|
||||||
|
from src.Logger.logger_db import LoggerDB, logger
|
||||||
|
LoggerDB(db_credencial, "logger_textos", created_by="sistema")
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
+22
-25
@@ -6,48 +6,50 @@ from src.ConexionSql.Postgres_conexion import PostgresConexion
|
|||||||
from src.TextManager.nota import Nota
|
from src.TextManager.nota import Nota
|
||||||
from src.TextManager.notas_mmr import generar_tabla_nota_para_biblioteca, NotaRepo
|
from src.TextManager.notas_mmr import generar_tabla_nota_para_biblioteca, NotaRepo
|
||||||
from sqlalchemy import MetaData
|
from sqlalchemy import MetaData
|
||||||
from backend.schemas.text_manager_schema import NotaInput
|
from backend.domains.text_manager.text_manager_schema import NotaInput
|
||||||
|
|
||||||
|
|
||||||
|
from entrypoint.init_db import db_credencial
|
||||||
|
from src.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):
|
def crear_biblioteca(nombre_biblioteca: str, conexion: PostgresConexion, descripcion: str = None):
|
||||||
print("[INICIO] Creando biblioteca...")
|
logger.info("[INICIO] Creando biblioteca...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("[Paso 1] Obteniendo credencial...")
|
logger.info("[Paso 1] Obteniendo credencial...")
|
||||||
cred_repo = OpenAICredencialRepo(conexion)
|
cred_repo = OpenAICredencialRepo(conexion)
|
||||||
credencial = cred_repo.get_by_id("OPAK20250513-61b29978b7604031014")
|
credencial = cred_repo.get_by_id("OPAK20250513-61b29978b7604031014")
|
||||||
print("[OK] Credencial obtenida:", credencial.titulo if credencial else "❌ None")
|
logger.debug(f"[OK] Credencial obtenida: {credencial.titulo if credencial else '❌ None'}")
|
||||||
|
|
||||||
print("[Paso 2] Instanciando embedder...")
|
logger.info("[Paso 2] Instanciando embedder...")
|
||||||
embedder = OpenAIEmbedder(credencial, model="text-embedding-3-large")
|
embedder = OpenAIEmbedder(credencial, model="text-embedding-3-large")
|
||||||
print("[OK] Embedder instanciado")
|
logger.debug("[OK] Embedder instanciado")
|
||||||
|
|
||||||
print("[Paso 3] Instanciando biblioteca...")
|
logger.info("[Paso 3] Instanciando biblioteca...")
|
||||||
biblioteca = Biblioteca(
|
biblioteca = Biblioteca(
|
||||||
nombre=nombre_biblioteca,
|
nombre=nombre_biblioteca,
|
||||||
embedder=embedder,
|
embedder=embedder,
|
||||||
descripcion=descripcion
|
descripcion=descripcion
|
||||||
)
|
)
|
||||||
print(f"[OK] Biblioteca instanciada con ID: {biblioteca.id}")
|
logger.debug(f"[OK] Biblioteca instanciada con ID: {biblioteca.id}")
|
||||||
|
|
||||||
print("[Paso 4] Guardando en base de datos...")
|
logger.info("[Paso 4] Guardando en base de datos...")
|
||||||
repo = BibliotecaRepo(conexion)
|
repo = BibliotecaRepo(conexion)
|
||||||
repo.add(biblioteca=biblioteca)
|
repo.add(biblioteca=biblioteca)
|
||||||
print("[OK] Biblioteca guardada")
|
logger.success("[OK] Biblioteca guardada")
|
||||||
|
|
||||||
print("[Paso 5] Generando modelo de notas...")
|
logger.info("[Paso 5] Generando modelo de notas...")
|
||||||
biblioteca.generar_modelo_notas(conexion)
|
biblioteca.generar_modelo_notas(conexion)
|
||||||
print("[OK] Modelo de notas generado")
|
logger.success("[OK] Modelo de notas generado")
|
||||||
|
|
||||||
print("[FIN] Biblioteca creada correctamente")
|
logger.success("[FIN] Biblioteca creada correctamente")
|
||||||
return {
|
return {
|
||||||
"mensaje": f"Biblioteca '{nombre_biblioteca}' creada con éxito.",
|
"mensaje": f"Biblioteca '{nombre_biblioteca}' creada con éxito.",
|
||||||
"id": biblioteca.id
|
"id": biblioteca.id
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("[ERROR] Ocurrió una excepción:", str(e))
|
logger.exception("[ERROR] Ocurrió una excepción:")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
@@ -64,6 +66,7 @@ def listar_bibliotecas(conexion: PostgresConexion) -> list[dict]:
|
|||||||
for b in bibliotecas
|
for b in bibliotecas
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def agregar_nota_a_biblioteca(
|
def agregar_nota_a_biblioteca(
|
||||||
conexion: PostgresConexion,
|
conexion: PostgresConexion,
|
||||||
biblioteca_id: str,
|
biblioteca_id: str,
|
||||||
@@ -73,25 +76,19 @@ def agregar_nota_a_biblioteca(
|
|||||||
conexiones: list[str] = None,
|
conexiones: list[str] = None,
|
||||||
resumen: str = ""
|
resumen: str = ""
|
||||||
):
|
):
|
||||||
|
|
||||||
# Obtener la biblioteca
|
|
||||||
repo_biblioteca = BibliotecaRepo(conexion)
|
repo_biblioteca = BibliotecaRepo(conexion)
|
||||||
biblioteca = repo_biblioteca.get_by_id(biblioteca_id)
|
biblioteca = repo_biblioteca.get_by_id(biblioteca_id)
|
||||||
if biblioteca is None:
|
if biblioteca is None:
|
||||||
raise ValueError(f"No se encontró la biblioteca con ID {biblioteca_id}")
|
raise ValueError(f"No se encontró la biblioteca con ID {biblioteca_id}")
|
||||||
|
|
||||||
# Crear objeto Nota
|
|
||||||
nota = Nota(
|
nota = Nota(
|
||||||
titulo=titulo,
|
titulo=titulo,
|
||||||
texto=texto,
|
texto=texto,
|
||||||
tags=tags or [],
|
tags=tags or [],
|
||||||
conexiones=conexiones or [],
|
conexiones=conexiones or [],
|
||||||
resumen=resumen or "",
|
resumen=resumen or "",
|
||||||
# vector=biblioteca.embedder.embed_text(texto),
|
|
||||||
# vector_resumen=biblioteca.embedder.embed_text(resumen) if resumen else None
|
|
||||||
)
|
)
|
||||||
# Mostrar atributos seguros
|
logger.debug(
|
||||||
print(
|
|
||||||
f"[DEBUG] Nota creada: titulo='{nota.titulo}', "
|
f"[DEBUG] Nota creada: titulo='{nota.titulo}', "
|
||||||
f"texto_len={len(nota.texto)}, "
|
f"texto_len={len(nota.texto)}, "
|
||||||
f"tags={len(nota.tags)}, "
|
f"tags={len(nota.tags)}, "
|
||||||
@@ -99,7 +96,6 @@ def agregar_nota_a_biblioteca(
|
|||||||
f"resumen_len={len(nota.resumen)}"
|
f"resumen_len={len(nota.resumen)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Preparar tabla y modelo de nota
|
|
||||||
metadata = MetaData()
|
metadata = MetaData()
|
||||||
tabla, ModeloNota = generar_tabla_nota_para_biblioteca(
|
tabla, ModeloNota = generar_tabla_nota_para_biblioteca(
|
||||||
biblioteca.nombre,
|
biblioteca.nombre,
|
||||||
@@ -108,7 +104,6 @@ def agregar_nota_a_biblioteca(
|
|||||||
)
|
)
|
||||||
metadata.create_all(conexion.get_engine())
|
metadata.create_all(conexion.get_engine())
|
||||||
|
|
||||||
# Guardar la nota
|
|
||||||
repo_nota = NotaRepo(conexion.get_session(), ModeloNota)
|
repo_nota = NotaRepo(conexion.get_session(), ModeloNota)
|
||||||
nota_id = repo_nota.add(nota)
|
nota_id = repo_nota.add(nota)
|
||||||
|
|
||||||
@@ -117,7 +112,7 @@ def agregar_nota_a_biblioteca(
|
|||||||
"nota_id": nota_id
|
"nota_id": nota_id
|
||||||
}
|
}
|
||||||
|
|
||||||
print(f"[SUCCESS] {resultado['mensaje']}")
|
logger.success(f"[SUCCESS] {resultado['mensaje']}")
|
||||||
return resultado
|
return resultado
|
||||||
|
|
||||||
|
|
||||||
@@ -160,6 +155,7 @@ def eliminar_nota(conexion: PostgresConexion, biblioteca_id: str, nota_id: str)
|
|||||||
fue_eliminada = repo_nota.delete_by_id(nota_id)
|
fue_eliminada = repo_nota.delete_by_id(nota_id)
|
||||||
|
|
||||||
if fue_eliminada:
|
if fue_eliminada:
|
||||||
|
logger.success(f"Nota '{nota_id}' eliminada correctamente.")
|
||||||
return {"mensaje": f"Nota '{nota_id}' eliminada correctamente."}
|
return {"mensaje": f"Nota '{nota_id}' eliminada correctamente."}
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"No se encontró la nota con ID: {nota_id}")
|
raise ValueError(f"No se encontró la nota con ID: {nota_id}")
|
||||||
@@ -186,6 +182,7 @@ def actualizar_nota(conexion: PostgresConexion, biblioteca_id: str, nota_id: str
|
|||||||
fue_actualizada = repo_nota.update(nota_id, nota_actualizada)
|
fue_actualizada = repo_nota.update(nota_id, nota_actualizada)
|
||||||
|
|
||||||
if fue_actualizada:
|
if fue_actualizada:
|
||||||
|
logger.success(f"Nota '{nota_id}' actualizada correctamente.")
|
||||||
return {"mensaje": f"Nota '{nota_id}' actualizada correctamente."}
|
return {"mensaje": f"Nota '{nota_id}' actualizada correctamente."}
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"No se encontró la nota con ID: {nota_id}")
|
raise ValueError(f"No se encontró la nota con ID: {nota_id}")
|
||||||
+2
-2
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from backend.api.v1.router import router
|
from backend.router_v1 import router
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Fitz Backend",
|
title="Fitz Backend",
|
||||||
@@ -21,4 +21,4 @@ app.add_middleware(
|
|||||||
|
|
||||||
|
|
||||||
# Incluye las rutas de tu API
|
# Incluye las rutas de tu API
|
||||||
app.include_router(router)
|
app.include_router(router, prefix="/api/v1", tags=["v1"])
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# backend/api/router_v1.py
|
||||||
|
|
||||||
|
from fastapi import APIRouter
|
||||||
|
from backend.domains.experiments import charts_examples_endpoint_v1 as charts
|
||||||
|
from backend.domains.experiments import ping_endpoint_v1
|
||||||
|
from backend.domains.text_manager import text_manager_endpoint_v1
|
||||||
|
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
router.include_router(ping_endpoint_v1.router, prefix="/ping")
|
||||||
|
router.include_router(text_manager_endpoint_v1.router, prefix="/text_manager")
|
||||||
|
router.include_router(charts.router, prefix="/charts")
|
||||||
Generated
+2984
-19
File diff suppressed because it is too large
Load Diff
+14
-3
@@ -20,16 +20,26 @@
|
|||||||
"storybook:build": "storybook build"
|
"storybook:build": "storybook build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@mantine/core": "8.0.0",
|
"@cycjimmy/jsmpeg-player": "^6.1.2",
|
||||||
"@mantine/hooks": "8.0.0",
|
"@mantine/core": "^8.0.1",
|
||||||
|
"@mantine/hooks": "^8.0.1",
|
||||||
|
"@mantine/tiptap": "^8.0.1",
|
||||||
"@react-three/fiber": "^9.1.2",
|
"@react-three/fiber": "^9.1.2",
|
||||||
"@tabler/icons": "^3.31.0",
|
"@tabler/icons": "^3.31.0",
|
||||||
"@tabler/icons-react": "^3.31.0",
|
"@tabler/icons-react": "^3.31.0",
|
||||||
|
"@tiptap/react": "^2.12.0",
|
||||||
|
"@tiptap/starter-kit": "^2.12.0",
|
||||||
|
"@uiw/react-markdown-preview": "^5.1.4",
|
||||||
|
"@uiw/react-md-editor": "^4.0.7",
|
||||||
"axios": "^1.9.0",
|
"axios": "^1.9.0",
|
||||||
|
"echarts": "^5.6.0",
|
||||||
|
"echarts-for-react": "^3.0.2",
|
||||||
|
"marked": "^15.0.12",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0",
|
"react-dom": "^19.1.0",
|
||||||
"react-rnd": "^10.5.2",
|
"react-rnd": "^10.5.2",
|
||||||
"react-router-dom": "^7.4.0"
|
"react-router-dom": "^7.4.0",
|
||||||
|
"turndown": "^7.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.23.0",
|
"@eslint/js": "^9.23.0",
|
||||||
@@ -44,6 +54,7 @@
|
|||||||
"@types/react": "^19.0.12",
|
"@types/react": "^19.0.12",
|
||||||
"@types/react-dom": "^19.0.4",
|
"@types/react-dom": "^19.0.4",
|
||||||
"@types/three": "^0.176.0",
|
"@types/three": "^0.176.0",
|
||||||
|
"@types/turndown": "^5.0.5",
|
||||||
"@vitejs/plugin-react": "^4.3.4",
|
"@vitejs/plugin-react": "^4.3.4",
|
||||||
"eslint": "^9.23.0",
|
"eslint": "^9.23.0",
|
||||||
"eslint-config-mantine": "^4.0.3",
|
"eslint-config-mantine": "^4.0.3",
|
||||||
|
|||||||
+60
-11
@@ -1,29 +1,78 @@
|
|||||||
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
|
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
|
||||||
import { HomePage } from './pages/Home.page';
|
import { HomePage } from './domains/Home/Home.page';
|
||||||
import { Consulta_API } from './pages/Consulta_api';
|
import { Consulta_API } from './domains/Experiments/Consulta_api';
|
||||||
import { Error_404 } from './pages/404'; // Ajusta si está en otra carpeta
|
import { Error_404 } from './domains/FitzStudio/404/404'; // Ajusta si está en otra carpeta
|
||||||
import { Grid_Dashboard } from './pages/Grid_dashboard'; // Ajusta si está en otra carpeta
|
import { Grid_Dashboard } from './domains/Experiments/Grid_dashboard'; // Ajusta si está en otra carpeta
|
||||||
import { Biblioteca } from './pages/Biblioteca';
|
import { Biblioteca } from './domains/TextEditor/Biblioteca';
|
||||||
|
import { VisualizacionesRandom } from './domains/Experiments/Visualizaciones_Random';
|
||||||
|
import { Camara_noir } from './domains/CamaraNoir/Camaras_noir';
|
||||||
|
import EditorTest from "./domains/TextEditor/Editor_Test";
|
||||||
|
import { ChatPage } from './domains/Llms/Chat/ChatPage';
|
||||||
|
|
||||||
const router = createBrowserRouter([
|
const router = createBrowserRouter([
|
||||||
|
|
||||||
|
// Home Principal
|
||||||
|
|
||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
element: <HomePage />,
|
element: <HomePage />,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Biblioteca
|
||||||
|
|
||||||
{
|
{
|
||||||
path: '/Consulta_API',
|
path: '/bibliot/Biblioteca',
|
||||||
element: <Consulta_API />,
|
element: <Biblioteca />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/Grid_Dashboard',
|
path: '/bibliot/editortest',
|
||||||
|
element: <EditorTest />,
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Chat LLM
|
||||||
|
|
||||||
|
{
|
||||||
|
path: '/llms/chat',
|
||||||
|
element: <ChatPage />,
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// CamaraNoir
|
||||||
|
|
||||||
|
{
|
||||||
|
path: '/camara/principal',
|
||||||
|
element: <Camara_noir />,
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Experimentos
|
||||||
|
|
||||||
|
{
|
||||||
|
path: '/experiments/Consulta_API',
|
||||||
|
element: <Consulta_API />,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
path: '/experiments/Grid_Dashboard',
|
||||||
element: <Grid_Dashboard />,
|
element: <Grid_Dashboard />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/Biblioteca',
|
path: '/experiments/Visualizaciones_Random',
|
||||||
element: <Biblioteca />,
|
element: <VisualizacionesRandom />,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// FitzStudio Pages -------------------------------------------------------
|
||||||
|
// Error 404
|
||||||
|
|
||||||
{
|
{
|
||||||
path: '*',
|
path: '*',
|
||||||
element: <Error_404 />,
|
element: <Error_404 />,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// https://tabler.io/icons
|
||||||
|
|
||||||
// OUTLINED
|
// OUTLINED
|
||||||
export { default as IconArrowLeft } from './outlined/arrow-left.svg?react';
|
export { default as IconArrowLeft } from './outlined/arrow-left.svg?react';
|
||||||
export { default as IconHomeOutline } from './outlined/home.svg?react';
|
export { default as IconHomeOutline } from './outlined/home.svg?react';
|
||||||
@@ -11,7 +13,10 @@ export { default as IconSettings } from './outlined/settings.svg?react';
|
|||||||
export { default as IconArrowBarLeft } from './outlined/arrow-bar-left.svg?react';
|
export { default as IconArrowBarLeft } from './outlined/arrow-bar-left.svg?react';
|
||||||
export { default as IconArrowBarRight } from './outlined/arrow-bar-right.svg?react';
|
export { default as IconArrowBarRight } from './outlined/arrow-bar-right.svg?react';
|
||||||
export { default as IconCheck } from './outlined/check.svg?react';
|
export { default as IconCheck } from './outlined/check.svg?react';
|
||||||
|
export { default as CameraPlus } from './outlined/camera-plus.svg?react';
|
||||||
|
export { default as Flask } from './outlined/flask.svg?react';
|
||||||
|
export { default as Users } from './outlined/users.svg?react';
|
||||||
|
export { default as IconNotebook } from './outlined/notebook.svg?react';
|
||||||
|
|
||||||
// FILLED
|
// FILLED
|
||||||
export { default as IconHomeFilled } from './filled/home.svg?react';
|
export { default as IconHomeFilled } from './filled/home.svg?react';
|
||||||
|
|||||||
@@ -4,17 +4,20 @@ import {
|
|||||||
IconDeviceDesktopAnalytics,
|
IconDeviceDesktopAnalytics,
|
||||||
IconFingerprint,
|
IconFingerprint,
|
||||||
IconGauge,
|
IconGauge,
|
||||||
|
IconNotebook,
|
||||||
IconHome2,
|
IconHome2,
|
||||||
IconSettings,
|
IconSettings,
|
||||||
IconUserOutline as IconUser,
|
IconUserOutline as IconUser,
|
||||||
|
CameraPlus,
|
||||||
|
Flask,
|
||||||
|
Users
|
||||||
} from '../assets/icons'; // ajusta según tu estructura de proyecto
|
} from '../assets/icons'; // ajusta según tu estructura de proyecto
|
||||||
|
|
||||||
export const mainLinksdata = [
|
export const mainLinksdata = [
|
||||||
{ icon: IconHome2, label: 'Home' },
|
{ icon: IconHome2, label: 'Home' },
|
||||||
{ icon: IconGauge, label: 'Dashboard' },
|
{ icon: IconNotebook, label: 'Biblioteca' },
|
||||||
{ icon: IconDeviceDesktopAnalytics, label: 'Analytics' },
|
{ icon: Users, label: 'AgentesLLMs' },
|
||||||
{ icon: IconCalendarStats, label: 'Releases' },
|
{ icon: CameraPlus, label: 'CameraNoir' },
|
||||||
{ icon: IconUser, label: 'Account' },
|
{ icon: Flask, label: 'Experimentos' },
|
||||||
{ icon: IconFingerprint, label: 'Security' },
|
|
||||||
{ icon: IconSettings, label: 'Settings' },
|
{ icon: IconSettings, label: 'Settings' },
|
||||||
];
|
];
|
||||||
@@ -1,35 +1,49 @@
|
|||||||
// src/data/submenuLinks.ts
|
// src/data/submenuLinks.ts
|
||||||
|
|
||||||
|
import { Biblioteca } from "@/domains/TextEditor/Biblioteca";
|
||||||
|
|
||||||
export const submenuLinks = {
|
export const submenuLinks = {
|
||||||
|
|
||||||
|
// Home Principal
|
||||||
|
|
||||||
Home: [
|
Home: [
|
||||||
{ label: 'Inicio', to: '/' },
|
{ label: 'Inicio', to: '/' },
|
||||||
{ label: 'Consulta Api', to: '/Consulta_API' },
|
|
||||||
{ label: 'Biblioteca', to: '/Biblioteca' },
|
|
||||||
|
|
||||||
],
|
],
|
||||||
Dashboard: [
|
|
||||||
{ label: 'Resumen', to: '/dashboard/resumen' },
|
// Biblioteca
|
||||||
{ label: 'Grid_Dashboard', to: '/Grid_Dashboard' },
|
|
||||||
{ label: 'Estadísticas', to: '/dashboard/estadisticas' },
|
Biblioteca: [
|
||||||
{ label: 'Usuarios', to: '/dashboard/usuarios' },
|
{ label: 'Biblioteca', to: '/bibliot/Biblioteca' },
|
||||||
|
{ label: 'test', to: '/bibliot/editortest' },
|
||||||
|
|
||||||
],
|
],
|
||||||
Analytics: [
|
|
||||||
{ label: 'Conversiones', to: '/analytics/conversiones' },
|
|
||||||
{ label: 'Tráfico', to: '/analytics/trafico' },
|
|
||||||
{ label: 'Tendencias', to: '/analytics/tendencias' },
|
// Experimentos
|
||||||
|
Experimentos: [
|
||||||
|
{ label: 'Consulta Api', to: '/experiments/Consulta_API' },
|
||||||
|
{ label: 'Visualizaciones_Random', to: '/experiments/Visualizaciones_Random' },
|
||||||
|
{ label: 'Grid_Dashboard', to: '/experiments/Grid_Dashboard' },
|
||||||
],
|
],
|
||||||
Releases: [
|
|
||||||
{ label: 'Notas de versión', to: '/releases/notas-de-version' },
|
// Camara
|
||||||
{ label: 'Historial', to: '/releases/historial' },
|
CameraNoir: [
|
||||||
|
{ label: 'Camara_principal', to: '/camara/principal' },
|
||||||
],
|
],
|
||||||
Account: [
|
|
||||||
{ label: 'Perfil', to: '/account/perfil' },
|
// LLms
|
||||||
{ label: 'Suscripciones', to: '/account/suscripciones' },
|
|
||||||
],
|
AgentesLLMs: [
|
||||||
Security: [
|
{ label: 'LLMs', to: '/llms' },
|
||||||
{ label: 'Contraseña', to: '/security/contraseña' },
|
{ label: 'Chat', to: '/llms/chat' },
|
||||||
{ label: '2FA', to: '/security/2fa' },
|
{ label: 'Documentos', to: '/llms/documentos' },
|
||||||
|
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
|
// Settings
|
||||||
Settings: [
|
Settings: [
|
||||||
{ label: 'Preferencias', to: '/settings/preferencias' },
|
{ label: 'Preferencias', to: '/settings/preferencias' },
|
||||||
{ label: 'Notificaciones', to: '/settings/notificaciones' },
|
{ label: 'Notificaciones', to: '/settings/notificaciones' },
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
|
import { Card, Text, Container } from '@mantine/core';
|
||||||
|
|
||||||
|
export function Camara_noir() {
|
||||||
|
return (
|
||||||
|
<AppShellWithMenu>
|
||||||
|
<Container
|
||||||
|
size="lg"
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'center',
|
||||||
|
gap: 16,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Card shadow="sm" padding="xl" radius="md" withBorder>
|
||||||
|
<Text size="lg" mb="md">
|
||||||
|
Cámara Noir en Vivo
|
||||||
|
</Text>
|
||||||
|
<img
|
||||||
|
src="http://10.8.0.9:8000/video"
|
||||||
|
alt="Stream MJPEG en vivo desde Raspberry Pi"
|
||||||
|
style={{
|
||||||
|
width: '640px',
|
||||||
|
height: '480px',
|
||||||
|
borderRadius: '8px',
|
||||||
|
border: '1px solid #ccc',
|
||||||
|
objectFit: 'cover',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Text size="sm" color="dimmed" mt="sm">
|
||||||
|
Transmisión MJPEG en vivo vía FastAPI / libcamera-vid
|
||||||
|
</Text>
|
||||||
|
</Card>
|
||||||
|
</Container>
|
||||||
|
</AppShellWithMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import { LlamadorAPI } from '../components/LlamadorAPI';
|
import { LlamadorAPI } from './LlamadorAPI';
|
||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
|
|
||||||
|
|
||||||
export function Consulta_API() {
|
export function Consulta_API() {
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
import { Grid } from '@mantine/core';
|
import { Grid } from '@mantine/core';
|
||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
import { GridDashboard } from '../components/Grid_dashboard';
|
import { GridDashboard } from './Grid_dashboard_component';
|
||||||
|
|
||||||
export function Grid_Dashboard() {
|
export function Grid_Dashboard() {
|
||||||
return (
|
return (
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
import { Select, Group } from '@mantine/core';
|
import { Select, Group } from '@mantine/core';
|
||||||
import { IconCheck } from '../assets/icons';
|
import { IconCheck } from '../../assets/icons';
|
||||||
|
|
||||||
interface MetodoSelectProps {
|
interface MetodoSelectProps {
|
||||||
metodo: string;
|
metodo: string;
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
|
import { Card, Grid, Title, Loader } from '@mantine/core';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import ReactECharts from 'echarts-for-react';
|
||||||
|
|
||||||
|
type ChartOption = any;
|
||||||
|
|
||||||
|
function useChartOption(endpoint: string) {
|
||||||
|
const [option, setOption] = useState<ChartOption | null>(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetch(`/api/v1/charts/${endpoint}`)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((json) => setOption(json))
|
||||||
|
.catch(console.error)
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}, [endpoint]);
|
||||||
|
|
||||||
|
return { option, loading };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function VisualizacionesRandom() {
|
||||||
|
const charts = [
|
||||||
|
{ title: 'Gráfico de barras', endpoint: 'bar' },
|
||||||
|
{ title: 'Gráfico de líneas', endpoint: 'line' },
|
||||||
|
{ title: 'Gráfico de pastel', endpoint: 'pie' },
|
||||||
|
{ title: 'Scatter plot', endpoint: 'scatter' },
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppShellWithMenu>
|
||||||
|
<Grid>
|
||||||
|
{charts.map(({ title, endpoint }, idx) => {
|
||||||
|
const { option, loading } = useChartOption(endpoint);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Grid.Col span={{ base: 12, sm: 6, md: 6, lg: 3 }} key={idx}>
|
||||||
|
<Card shadow="sm" padding="lg" radius="md" withBorder>
|
||||||
|
<Title order={4}>{title}</Title>
|
||||||
|
{loading || !option ? (
|
||||||
|
<Loader mt="md" />
|
||||||
|
) : (
|
||||||
|
<ReactECharts option={option} style={{ height: 250, marginTop: 16 }} />
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</Grid.Col>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</Grid>
|
||||||
|
</AppShellWithMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import { Box, Title, Text, Button, Group, Stack, Image, Center } from '@mantine/core';
|
import { Box, Title, Text, Button, Group, Stack, Image, Center } from '@mantine/core';
|
||||||
import { useMantineTheme } from '@mantine/core';
|
import { useMantineTheme } from '@mantine/core';
|
||||||
import { IconArrowLeft } from '../assets/icons';
|
import { IconArrowLeft } from '../../../assets/icons';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { MantineCardWithShader } from '../components/HoloShader'; // Ajusta ruta si es necesario
|
import { MantineCardWithShader } from './HoloShader_404'; // Ajusta ruta si es necesario
|
||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
import { AppShellWithMenu } from '../../FitzStudio/Appshell/Appshell';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export function Error_404() {
|
export function Error_404() {
|
||||||
+3
-3
@@ -11,9 +11,9 @@ import { useDisclosure, useMediaQuery } from '@mantine/hooks';
|
|||||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { Link, useLocation, useNavigate } from 'react-router-dom';
|
import { Link, useLocation, useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import { default as LogoIcon } from '../../assets/icons/favicon';
|
import { default as LogoIcon } from '../../../assets/icons/favicon';
|
||||||
import { mainLinksdata } from '../../data/navigationsLinks_1';
|
import { mainLinksdata } from '../../../data/navigationsLinks_1';
|
||||||
import { submenuLinks } from '../../data/submenuLinks_1';
|
import { submenuLinks } from '../../../data/submenuLinks_1';
|
||||||
|
|
||||||
import classes from './Appshell.module.css';
|
import classes from './Appshell.module.css';
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
|
|
||||||
|
|
||||||
export function Plantilla() {
|
export function Plantilla() {
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
import { Welcome } from '@/components/Welcome/Welcome';
|
import { Welcome } from '@/domains/FitzStudio/Welcome/Welcome';
|
||||||
import { ColorSchemeToggle } from '@/components/ColorSchemeToggle/ColorSchemeToggle';
|
import { ColorSchemeToggle } from '@/domains/FitzStudio/ColorSchemeToggle/ColorSchemeToggle';
|
||||||
|
|
||||||
|
|
||||||
export function HomePage() {
|
export function HomePage() {
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { Textarea, Button, Group } from "@mantine/core";
|
||||||
|
|
||||||
|
export function ChatInput({ onSend }: { onSend: (text: string) => void }) {
|
||||||
|
const [text, setText] = useState("");
|
||||||
|
|
||||||
|
const handleSend = () => {
|
||||||
|
if (!text.trim()) return;
|
||||||
|
onSend(text.trim());
|
||||||
|
setText("");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Group>
|
||||||
|
<Textarea
|
||||||
|
value={text}
|
||||||
|
onChange={(e) => setText(e.currentTarget.value)}
|
||||||
|
autosize
|
||||||
|
minRows={1}
|
||||||
|
maxRows={4}
|
||||||
|
placeholder="Escribe tu mensaje..."
|
||||||
|
style={{ flex: 1 }}
|
||||||
|
/>
|
||||||
|
<Button onClick={handleSend}>Enviar</Button>
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { Container, Stack, Paper, ScrollArea, Title } from "@mantine/core";
|
||||||
|
import { ChatInput } from "./ChatInput";
|
||||||
|
import { MessageList } from "./MessageList";
|
||||||
|
import { AppShellWithMenu } from "../../FitzStudio/Appshell/Appshell";
|
||||||
|
|
||||||
|
export function ChatPage() {
|
||||||
|
const [messages, setMessages] = useState([
|
||||||
|
{ sender: "bot", content: "Hola, ¿en qué puedo ayudarte hoy?" },
|
||||||
|
]);
|
||||||
|
|
||||||
|
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" },
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
setMessages([...newMessages, { sender: "bot", content: data.reply }]);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppShellWithMenu>
|
||||||
|
<Container size="sm" p="md">
|
||||||
|
<Stack>
|
||||||
|
<Title order={2}>Chat LLM</Title>
|
||||||
|
<Paper shadow="xs" p="md" withBorder>
|
||||||
|
<ScrollArea h={400}>
|
||||||
|
<MessageList messages={messages} />
|
||||||
|
</ScrollArea>
|
||||||
|
</Paper>
|
||||||
|
<ChatInput onSend={handleSend} />
|
||||||
|
</Stack>
|
||||||
|
</Container>
|
||||||
|
</AppShellWithMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { Paper, Text, useMantineTheme, useMantineColorScheme } from "@mantine/core";
|
||||||
|
|
||||||
|
export function MessageBubble({ sender, content }: { sender: string; content: string }) {
|
||||||
|
const theme = useMantineTheme();
|
||||||
|
const { colorScheme } = useMantineColorScheme();
|
||||||
|
const isUser = sender === "user";
|
||||||
|
|
||||||
|
const userBg = theme.colors[theme.primaryColor][0];
|
||||||
|
const botBg = colorScheme === "dark" ? theme.colors.dark[6] : theme.colors.gray[0];
|
||||||
|
const userTextColor = theme.colors[theme.primaryColor][9];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Paper
|
||||||
|
p="sm"
|
||||||
|
radius="md"
|
||||||
|
withBorder
|
||||||
|
style={{
|
||||||
|
alignSelf: isUser ? "flex-end" : "flex-start",
|
||||||
|
backgroundColor: isUser ? userBg : botBg,
|
||||||
|
maxWidth: "80%",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Text size="sm" c={isUser ? userTextColor : undefined}>
|
||||||
|
{content}
|
||||||
|
</Text>
|
||||||
|
</Paper>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { Stack } from "@mantine/core";
|
||||||
|
import { MessageBubble } from "./MessageBubble";
|
||||||
|
|
||||||
|
export function MessageList({ messages }: { messages: { sender: string; content: string }[] }) {
|
||||||
|
return (
|
||||||
|
<Stack>
|
||||||
|
{messages.map((msg, i) => (
|
||||||
|
<MessageBubble key={i} sender={msg.sender} content={msg.content} />
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,293 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import {
|
||||||
|
Stack,
|
||||||
|
Text,
|
||||||
|
Title,
|
||||||
|
ScrollArea,
|
||||||
|
Group,
|
||||||
|
Button,
|
||||||
|
TextInput,
|
||||||
|
Modal,
|
||||||
|
Box
|
||||||
|
} from '@mantine/core';
|
||||||
|
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
import { RichTextEditor } from '@mantine/tiptap';
|
||||||
|
import { useEditor } from '@tiptap/react';
|
||||||
|
import StarterKit from '@tiptap/starter-kit';
|
||||||
|
import '@mantine/tiptap/styles.css';
|
||||||
|
|
||||||
|
import TurndownService from 'turndown';
|
||||||
|
import { marked } from 'marked';
|
||||||
|
|
||||||
|
import './Editor_biblioteca.css';
|
||||||
|
|
||||||
|
type Nota = {
|
||||||
|
id: string;
|
||||||
|
titulo: string;
|
||||||
|
texto: string; // Markdown
|
||||||
|
};
|
||||||
|
|
||||||
|
type Biblioteca = {
|
||||||
|
id: string;
|
||||||
|
nombre: string;
|
||||||
|
descripcion: string;
|
||||||
|
notas: Nota[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const turndownService = new TurndownService({
|
||||||
|
headingStyle: 'atx',
|
||||||
|
bulletListMarker: '-',
|
||||||
|
codeBlockStyle: 'fenced',
|
||||||
|
emDelimiter: '*',
|
||||||
|
strongDelimiter: '**',
|
||||||
|
});
|
||||||
|
|
||||||
|
export function Biblioteca() {
|
||||||
|
const [bibliotecas, setBibliotecas] = useState<Biblioteca[]>([]);
|
||||||
|
const [bibliotecaSeleccionada, setBibliotecaSeleccionada] = useState<Biblioteca | null>(null);
|
||||||
|
const [notaSeleccionada, setNotaSeleccionada] = useState<Nota | null>(null);
|
||||||
|
const [modalNuevaBiblio, setModalNuevaBiblio] = useState(false);
|
||||||
|
const [nombreBiblio, setNombreBiblio] = useState('');
|
||||||
|
const [descripcionBiblio, setDescripcionBiblio] = useState('');
|
||||||
|
const [loadingNuevaBiblio, setLoadingNuevaBiblio] = useState(false);
|
||||||
|
|
||||||
|
|
||||||
|
const editor = useEditor({
|
||||||
|
extensions: [StarterKit],
|
||||||
|
content: '',
|
||||||
|
onUpdate: ({ editor }) => {
|
||||||
|
const html = editor.getHTML();
|
||||||
|
const markdown = turndownService.turndown(html);
|
||||||
|
setNotaSeleccionada((prev) =>
|
||||||
|
prev ? { ...prev, texto: markdown } : null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('🟡 editor:', editor);
|
||||||
|
console.log('🟠 isDestroyed:', editor?.isDestroyed);
|
||||||
|
console.log('🟢 isEditable:', editor?.isEditable);
|
||||||
|
}, [editor]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchBibliotecas();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!editor || !notaSeleccionada || editor.isDestroyed) return;
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const markdown = notaSeleccionada.texto;
|
||||||
|
const html = await marked.parse(markdown);
|
||||||
|
editor.commands.setContent(html);
|
||||||
|
setTimeout(() => editor.commands.focus(), 100);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('❌ Error al hacer setContent:', err);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, [notaSeleccionada?.id, editor]);
|
||||||
|
|
||||||
|
const fetchBibliotecas = async () => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get('/api/v1/text_manager/list');
|
||||||
|
if (!Array.isArray(res.data)) return;
|
||||||
|
|
||||||
|
const bibliotecasConNotas = await Promise.all(
|
||||||
|
res.data.map(async (biblio: Omit<Biblioteca, 'notas'>) => {
|
||||||
|
const notas = await axios.get(`/api/v1/text_manager/nota/list/${biblio.id}`);
|
||||||
|
return { ...biblio, notas: notas.data as Nota[] };
|
||||||
|
})
|
||||||
|
);
|
||||||
|
setBibliotecas(bibliotecasConNotas);
|
||||||
|
setBibliotecaSeleccionada(bibliotecasConNotas[0] || null);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error al cargar bibliotecas:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const crearBiblioteca = async () => {
|
||||||
|
setLoadingNuevaBiblio(true);
|
||||||
|
try {
|
||||||
|
await axios.post('/api/v1/text_manager/biblioteca', {
|
||||||
|
nombre_biblioteca: nombreBiblio,
|
||||||
|
descripcion: descripcionBiblio,
|
||||||
|
});
|
||||||
|
setNombreBiblio('');
|
||||||
|
setDescripcionBiblio('');
|
||||||
|
setModalNuevaBiblio(false);
|
||||||
|
await fetchBibliotecas();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error al crear biblioteca:', error);
|
||||||
|
} finally {
|
||||||
|
setLoadingNuevaBiblio(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const guardarEdicionNota = async () => {
|
||||||
|
if (!notaSeleccionada || !bibliotecaSeleccionada) return;
|
||||||
|
try {
|
||||||
|
await axios.put(`/api/v1/text_manager/nota/${bibliotecaSeleccionada.id}/${notaSeleccionada.id}`, {
|
||||||
|
titulo: notaSeleccionada.titulo,
|
||||||
|
texto: notaSeleccionada.texto,
|
||||||
|
tags: [],
|
||||||
|
conexiones: [],
|
||||||
|
resumen: ""
|
||||||
|
});
|
||||||
|
await fetchBibliotecas();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error al actualizar nota:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const eliminarNota = async (notaId: string) => {
|
||||||
|
if (!bibliotecaSeleccionada) return;
|
||||||
|
try {
|
||||||
|
await axios.delete(`/api/v1/text_manager/nota/${bibliotecaSeleccionada.id}/${notaId}`);
|
||||||
|
await fetchBibliotecas();
|
||||||
|
setNotaSeleccionada(null);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error al eliminar nota:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppShellWithMenu>
|
||||||
|
<Box display="flex" h="100%" style={{ overflow: 'hidden' }}>
|
||||||
|
<Box w={240} p="md">
|
||||||
|
<ScrollArea h="100%">
|
||||||
|
<Stack gap="md">
|
||||||
|
<Button color="teal" onClick={fetchBibliotecas}>🔄 Recuperar bibliotecas</Button>
|
||||||
|
<Button color="grape" variant="outline" onClick={() => setModalNuevaBiblio(true)}>➕ Nueva biblioteca</Button>
|
||||||
|
{bibliotecas.map((biblio) => (
|
||||||
|
<Button
|
||||||
|
key={biblio.id}
|
||||||
|
size="xs"
|
||||||
|
fullWidth
|
||||||
|
variant={biblio.id === bibliotecaSeleccionada?.id ? 'filled' : 'light'}
|
||||||
|
color="blue"
|
||||||
|
onClick={() => {
|
||||||
|
setBibliotecaSeleccionada(biblio);
|
||||||
|
setNotaSeleccionada(null);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{biblio.nombre}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
</ScrollArea>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<Box w={240} p="md">
|
||||||
|
<ScrollArea h="100%">
|
||||||
|
<Stack gap="md">
|
||||||
|
<Title order={4}>Notas</Title>
|
||||||
|
<Button
|
||||||
|
color="green"
|
||||||
|
variant="outline"
|
||||||
|
fullWidth
|
||||||
|
onClick={async () => {
|
||||||
|
if (!bibliotecaSeleccionada) return;
|
||||||
|
try {
|
||||||
|
const res = await axios.post(`/api/v1/text_manager/nota/${bibliotecaSeleccionada.id}`, {
|
||||||
|
titulo: 'Nueva nota',
|
||||||
|
texto: '',
|
||||||
|
tags: [],
|
||||||
|
conexiones: [],
|
||||||
|
resumen: ''
|
||||||
|
});
|
||||||
|
const nuevaNota: Nota = res.data;
|
||||||
|
await fetchBibliotecas();
|
||||||
|
setNotaSeleccionada(nuevaNota);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error al crear nota:', error);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
➕ Nueva nota
|
||||||
|
</Button>
|
||||||
|
{bibliotecaSeleccionada?.notas.map((nota) => (
|
||||||
|
<Button
|
||||||
|
key={nota.id}
|
||||||
|
fullWidth
|
||||||
|
variant={notaSeleccionada?.id === nota.id ? 'filled' : 'light'}
|
||||||
|
color="gray"
|
||||||
|
onClick={() => setNotaSeleccionada(nota)}
|
||||||
|
>
|
||||||
|
{nota.titulo}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
</ScrollArea>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<Box p="md" style={{ flex: 1, overflow: 'hidden', minWidth: 0 }}>
|
||||||
|
{notaSeleccionada ? (
|
||||||
|
<Stack gap="sm">
|
||||||
|
<TextInput
|
||||||
|
label="Título"
|
||||||
|
size="lg"
|
||||||
|
styles={{ input: { fontSize: 20, fontWeight: 600 } }}
|
||||||
|
value={notaSeleccionada.titulo}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNotaSeleccionada((prev) => prev ? { ...prev, titulo: e.currentTarget.value } : null)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{editor && !editor.isDestroyed && (
|
||||||
|
<RichTextEditor
|
||||||
|
editor={editor}
|
||||||
|
miw={0}
|
||||||
|
style={{ fontSize: 14, minHeight: 200 }}
|
||||||
|
classNames={{ content: 'tiptap' }}
|
||||||
|
>
|
||||||
|
<RichTextEditor.Toolbar sticky stickyOffset={0}>
|
||||||
|
<RichTextEditor.ControlsGroup>
|
||||||
|
<RichTextEditor.Bold />
|
||||||
|
<RichTextEditor.Italic />
|
||||||
|
<RichTextEditor.Strikethrough />
|
||||||
|
<RichTextEditor.ClearFormatting />
|
||||||
|
<RichTextEditor.H1 />
|
||||||
|
<RichTextEditor.H2 />
|
||||||
|
<RichTextEditor.Blockquote />
|
||||||
|
<RichTextEditor.CodeBlock />
|
||||||
|
</RichTextEditor.ControlsGroup>
|
||||||
|
</RichTextEditor.Toolbar>
|
||||||
|
{/* tabIndex removido */}
|
||||||
|
<RichTextEditor.Content className="tiptap" />
|
||||||
|
</RichTextEditor>
|
||||||
|
)}
|
||||||
|
<Group mt="sm">
|
||||||
|
<Button color="blue" onClick={guardarEdicionNota}>💾 Guardar</Button>
|
||||||
|
<Button color="red" onClick={() => eliminarNota(notaSeleccionada.id)}>🗑️ Eliminar</Button>
|
||||||
|
</Group>
|
||||||
|
</Stack>
|
||||||
|
) : (
|
||||||
|
<Text>Selecciona una nota para editar</Text>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<Modal opened={modalNuevaBiblio} onClose={() => setModalNuevaBiblio(false)} title="Crear nueva biblioteca">
|
||||||
|
<Stack gap="md">
|
||||||
|
<TextInput
|
||||||
|
label="Nombre"
|
||||||
|
value={nombreBiblio}
|
||||||
|
onChange={(e) => setNombreBiblio(e.currentTarget.value)}
|
||||||
|
disabled={loadingNuevaBiblio}
|
||||||
|
/>
|
||||||
|
<TextInput
|
||||||
|
label="Descripción"
|
||||||
|
value={descripcionBiblio}
|
||||||
|
onChange={(e) => setDescripcionBiblio(e.currentTarget.value)}
|
||||||
|
disabled={loadingNuevaBiblio}
|
||||||
|
/>
|
||||||
|
<Button onClick={crearBiblioteca} loading={loadingNuevaBiblio}>Crear</Button>
|
||||||
|
</Stack>
|
||||||
|
</Modal>
|
||||||
|
</AppShellWithMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { RichTextEditor } from '@mantine/tiptap';
|
||||||
|
import { useEditor } from '@tiptap/react';
|
||||||
|
import StarterKit from '@tiptap/starter-kit';
|
||||||
|
import '@mantine/tiptap/styles.css';
|
||||||
|
|
||||||
|
export default function EditorTest() {
|
||||||
|
const editor = useEditor({
|
||||||
|
extensions: [StarterKit],
|
||||||
|
content: '<p>Prueba aquí. Presiona ENTER o ESPACIO.</p>',
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ padding: 40 }}>
|
||||||
|
{editor && (
|
||||||
|
<RichTextEditor editor={editor}>
|
||||||
|
<RichTextEditor.Toolbar sticky stickyOffset={0}>
|
||||||
|
<RichTextEditor.ControlsGroup>
|
||||||
|
<RichTextEditor.Bold />
|
||||||
|
<RichTextEditor.Italic />
|
||||||
|
</RichTextEditor.ControlsGroup>
|
||||||
|
</RichTextEditor.Toolbar>
|
||||||
|
<RichTextEditor.Content />
|
||||||
|
</RichTextEditor>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/* Editor_biblioteca.css */
|
||||||
|
/* En Editor_biblioteca.css */
|
||||||
|
.tiptap {
|
||||||
|
min-height: 200px;
|
||||||
|
padding: 8px;
|
||||||
|
/* white-space: pre-wrap; */
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tiptap p {
|
||||||
|
margin: 0 !important;
|
||||||
|
padding: 0 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tiptap h1, .tiptap h2, .tiptap h3 {
|
||||||
|
margin-top: 0.8em;
|
||||||
|
margin-bottom: 0.4em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tiptap blockquote {
|
||||||
|
margin: 0.6em 0;
|
||||||
|
/* padding-left: 1em; */
|
||||||
|
border-left: 3px solid #888;
|
||||||
|
color: #aaa;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mantine-RichTextEditor-toolbar {
|
||||||
|
background-color: #1e1e1e; /* o el color de tu layout */
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 6px 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mantine-RichTextEditor-controlIcon {
|
||||||
|
color: white !important;
|
||||||
|
stroke: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mantine-RichTextEditor-control {
|
||||||
|
background-color: transparent !important;
|
||||||
|
border: none !important;
|
||||||
|
box-shadow: none !important;
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export function Prueba_1() {
|
|
||||||
return (
|
|
||||||
<AppShellWithMenu>
|
|
||||||
|
|
||||||
</AppShellWithMenu>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,347 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import {
|
|
||||||
AppShell,
|
|
||||||
Stack,
|
|
||||||
Card,
|
|
||||||
Text,
|
|
||||||
Title,
|
|
||||||
ScrollArea,
|
|
||||||
Group,
|
|
||||||
Button,
|
|
||||||
TextInput,
|
|
||||||
Modal,
|
|
||||||
Box,
|
|
||||||
Loader,
|
|
||||||
Textarea
|
|
||||||
} from '@mantine/core';
|
|
||||||
import { AppShellWithMenu } from '../components/Appshell/Appshell';
|
|
||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
type Nota = {
|
|
||||||
id: string;
|
|
||||||
titulo: string;
|
|
||||||
texto: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type Biblioteca = {
|
|
||||||
id: string;
|
|
||||||
nombre: string;
|
|
||||||
descripcion: string;
|
|
||||||
notas: Nota[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export function Biblioteca() {
|
|
||||||
const [bibliotecas, setBibliotecas] = useState<Biblioteca[]>([]);
|
|
||||||
const [bibliotecaSeleccionada, setBibliotecaSeleccionada] = useState<Biblioteca | null>(null);
|
|
||||||
const [modalAbierto, setModalAbierto] = useState(false);
|
|
||||||
const [tituloNota, setTituloNota] = useState('');
|
|
||||||
const [contenidoNota, setContenidoNota] = useState('');
|
|
||||||
const [loadingNotas, setLoadingNotas] = useState(false);
|
|
||||||
const [notaEnEdicion, setNotaEnEdicion] = useState<Nota | null>(null);
|
|
||||||
const [modalEditarAbierto, setModalEditarAbierto] = useState(false);
|
|
||||||
const [modalNuevaBiblio, setModalNuevaBiblio] = useState(false);
|
|
||||||
const [nombreBiblio, setNombreBiblio] = useState('');
|
|
||||||
const [descripcionBiblio, setDescripcionBiblio] = useState('');
|
|
||||||
const [loadingNuevaBiblio, setLoadingNuevaBiblio] = useState(false);
|
|
||||||
|
|
||||||
|
|
||||||
const fetchBibliotecas = async () => {
|
|
||||||
try {
|
|
||||||
const res = await axios.get('/api/v1/text_manager/list');
|
|
||||||
console.log('📦 Respuesta del backend:', res.data);
|
|
||||||
|
|
||||||
if (!Array.isArray(res.data)) {
|
|
||||||
console.error('❌ La respuesta no es un array:', res.data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const bibliotecasConNotas = await Promise.all(
|
|
||||||
res.data.map(async (biblio: Omit<Biblioteca, 'notas'>) => {
|
|
||||||
const notas = await axios.get(`/api/v1/text_manager/nota/list/${biblio.id}`);
|
|
||||||
return { ...biblio, notas: notas.data as Nota[] };
|
|
||||||
})
|
|
||||||
);
|
|
||||||
setBibliotecas(bibliotecasConNotas);
|
|
||||||
setBibliotecaSeleccionada(bibliotecasConNotas[0] || null);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error al cargar bibliotecas:', error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const crearBiblioteca = async () => {
|
|
||||||
setLoadingNuevaBiblio(true); // 🔄 Activa el loader en el botón
|
|
||||||
try {
|
|
||||||
// Llamada a backend
|
|
||||||
await axios.post('/api/v1/text_manager/biblioteca', {
|
|
||||||
nombre_biblioteca: nombreBiblio,
|
|
||||||
descripcion: descripcionBiblio,
|
|
||||||
});
|
|
||||||
|
|
||||||
// 🧼 Limpia formularios
|
|
||||||
setNombreBiblio('');
|
|
||||||
setDescripcionBiblio('');
|
|
||||||
|
|
||||||
// 🔒 Cierra el modal
|
|
||||||
setModalNuevaBiblio(false);
|
|
||||||
|
|
||||||
// 🔄 Refresca la lista de bibliotecas
|
|
||||||
await fetchBibliotecas();
|
|
||||||
} catch (error) {
|
|
||||||
console.error('❌ Error al crear biblioteca:', error);
|
|
||||||
} finally {
|
|
||||||
setLoadingNuevaBiblio(false); // ✅ Apaga el loader
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const agregarNota = async () => {
|
|
||||||
if (!bibliotecaSeleccionada) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await axios.post(`/api/v1/text_manager/nota/${bibliotecaSeleccionada.id}`, {
|
|
||||||
titulo: tituloNota,
|
|
||||||
texto: contenidoNota,
|
|
||||||
tags: [],
|
|
||||||
conexiones: [],
|
|
||||||
resumen: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
setLoadingNotas(true);
|
|
||||||
const nuevasNotas = await axios.get(`/api/v1/text_manager/nota/list/${bibliotecaSeleccionada.id}`);
|
|
||||||
const nuevasBibliotecas = bibliotecas.map((b) =>
|
|
||||||
b.id === bibliotecaSeleccionada.id ? { ...b, notas: nuevasNotas.data as Nota[] } : b
|
|
||||||
);
|
|
||||||
setBibliotecas(nuevasBibliotecas);
|
|
||||||
setBibliotecaSeleccionada(nuevasBibliotecas.find((b) => b.id === bibliotecaSeleccionada.id) || null);
|
|
||||||
setTituloNota('');
|
|
||||||
setContenidoNota('');
|
|
||||||
setModalAbierto(false);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error al agregar nota:', error);
|
|
||||||
} finally {
|
|
||||||
setLoadingNotas(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
fetchBibliotecas();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Eliminar nota
|
|
||||||
const eliminarNota = async (notaId: string) => {
|
|
||||||
if (!bibliotecaSeleccionada) return;
|
|
||||||
try {
|
|
||||||
await axios.delete(`/api/v1/text_manager/nota/${bibliotecaSeleccionada.id}/${notaId}`);
|
|
||||||
|
|
||||||
// Solo actualiza la biblioteca actual
|
|
||||||
const nuevasNotas = await axios.get(`/api/v1/text_manager/nota/list/${bibliotecaSeleccionada.id}`);
|
|
||||||
const nuevasBibliotecas = bibliotecas.map((b) =>
|
|
||||||
b.id === bibliotecaSeleccionada.id ? { ...b, notas: nuevasNotas.data as Nota[] } : b
|
|
||||||
);
|
|
||||||
setBibliotecas(nuevasBibliotecas);
|
|
||||||
setBibliotecaSeleccionada(nuevasBibliotecas.find(b => b.id === bibliotecaSeleccionada.id) || null);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error al eliminar nota:", error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Editar nota
|
|
||||||
const abrirModalEditar = (nota: Nota) => {
|
|
||||||
setNotaEnEdicion(nota);
|
|
||||||
setModalEditarAbierto(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Guardar cambios de edición
|
|
||||||
const guardarEdicionNota = async () => {
|
|
||||||
if (!notaEnEdicion || !bibliotecaSeleccionada) return;
|
|
||||||
try {
|
|
||||||
await axios.put(`/api/v1/text_manager/nota/${bibliotecaSeleccionada.id}/${notaEnEdicion.id}`, {
|
|
||||||
titulo: notaEnEdicion.titulo,
|
|
||||||
texto: notaEnEdicion.texto,
|
|
||||||
tags: [],
|
|
||||||
conexiones: [],
|
|
||||||
resumen: ""
|
|
||||||
});
|
|
||||||
setModalEditarAbierto(false);
|
|
||||||
setNotaEnEdicion(null);
|
|
||||||
await fetchBibliotecas();
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error al actualizar nota:", error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AppShellWithMenu>
|
|
||||||
<Box display="flex" h="100%">
|
|
||||||
<Box w={240} p="md">
|
|
||||||
<ScrollArea h="100%">
|
|
||||||
<Stack>
|
|
||||||
<Button color="teal" onClick={fetchBibliotecas}>🔄 Recuperar bibliotecas</Button>
|
|
||||||
<Button color="grape" variant="outline" onClick={() => setModalNuevaBiblio(true)}>➕ Nueva biblioteca</Button>
|
|
||||||
|
|
||||||
|
|
||||||
{bibliotecas.map((biblio) => (
|
|
||||||
<Button
|
|
||||||
key={biblio.id}
|
|
||||||
size="xs"
|
|
||||||
fullWidth
|
|
||||||
variant={biblio.id === bibliotecaSeleccionada?.id ? 'filled' : 'light'}
|
|
||||||
color="blue"
|
|
||||||
onClick={() => setBibliotecaSeleccionada(biblio)}
|
|
||||||
>
|
|
||||||
{biblio.nombre}
|
|
||||||
</Button>
|
|
||||||
))}
|
|
||||||
</Stack>
|
|
||||||
</ScrollArea>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
<Box p="md" style={{ flex: 1 }}>
|
|
||||||
{bibliotecaSeleccionada ? (
|
|
||||||
<Stack>
|
|
||||||
<Title order={2}>{bibliotecaSeleccionada.nombre}</Title>
|
|
||||||
<Group>
|
|
||||||
<Button onClick={() => setModalAbierto(true)}>Agregar nota</Button>
|
|
||||||
</Group>
|
|
||||||
<Group>
|
|
||||||
{loadingNotas ? (
|
|
||||||
<Loader />
|
|
||||||
) : (
|
|
||||||
bibliotecaSeleccionada.notas.map((nota) => (
|
|
||||||
|
|
||||||
// Cards de notas
|
|
||||||
|
|
||||||
<Card
|
|
||||||
key={nota.id}
|
|
||||||
shadow="sm"
|
|
||||||
padding="lg"
|
|
||||||
radius="md"
|
|
||||||
withBorder
|
|
||||||
style={{
|
|
||||||
width: 300,
|
|
||||||
height: 250, // Altura fija para asegurar la separación
|
|
||||||
display: 'flex',
|
|
||||||
flexDirection: 'column',
|
|
||||||
justifyContent: 'space-between',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div>
|
|
||||||
<Title order={4} style={{ marginBottom: 10 }}>{nota.titulo}</Title>
|
|
||||||
<Text>{nota.texto}</Text>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Box mt="md" style={{ display: 'flex', justifyContent: 'flex-end' }}>
|
|
||||||
<Button
|
|
||||||
size="xs"
|
|
||||||
variant="light"
|
|
||||||
color="blue"
|
|
||||||
onClick={() => abrirModalEditar(nota)}
|
|
||||||
>
|
|
||||||
Editar
|
|
||||||
</Button>
|
|
||||||
</Box>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
// Fin de notas en cards
|
|
||||||
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
</Group>
|
|
||||||
</Stack>
|
|
||||||
) : (
|
|
||||||
<Stack>
|
|
||||||
<Text>Selecciona una biblioteca</Text>
|
|
||||||
|
|
||||||
</Stack>
|
|
||||||
)}
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/* Modal para agregar */}
|
|
||||||
<Modal opened={modalAbierto} onClose={() => setModalAbierto(false)} title="Agregar nueva nota">
|
|
||||||
<Stack>
|
|
||||||
<TextInput
|
|
||||||
label="Título"
|
|
||||||
value={tituloNota}
|
|
||||||
onChange={(event) => setTituloNota(event.currentTarget.value)}
|
|
||||||
/>
|
|
||||||
<Textarea
|
|
||||||
label="Contenido"
|
|
||||||
minRows={6}
|
|
||||||
autosize
|
|
||||||
value={contenidoNota}
|
|
||||||
onChange={(event) => setContenidoNota(event.currentTarget.value)}
|
|
||||||
/>
|
|
||||||
<Button onClick={agregarNota}>Guardar</Button>
|
|
||||||
</Stack>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
{/* Modal para editar */}
|
|
||||||
<Modal opened={modalEditarAbierto} onClose={() => setModalEditarAbierto(false)} title="Editar nota">
|
|
||||||
<Stack>
|
|
||||||
<TextInput
|
|
||||||
label="Título"
|
|
||||||
value={notaEnEdicion?.titulo || ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
setNotaEnEdicion((prev) => (prev ? { ...prev, titulo: e.currentTarget.value } : null))
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<Textarea
|
|
||||||
label="Contenido"
|
|
||||||
minRows={6}
|
|
||||||
autosize
|
|
||||||
value={notaEnEdicion?.texto || ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
setNotaEnEdicion((prev) => (prev ? { ...prev, texto: e.currentTarget.value } : null))
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<Group grow>
|
|
||||||
<Button color="blue" onClick={guardarEdicionNota}>
|
|
||||||
💾 Guardar cambios
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
color="red"
|
|
||||||
onClick={async () => {
|
|
||||||
if (!notaEnEdicion || !bibliotecaSeleccionada) return;
|
|
||||||
await eliminarNota(notaEnEdicion.id);
|
|
||||||
setModalEditarAbierto(false);
|
|
||||||
setNotaEnEdicion(null);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
🗑️ Eliminar nota
|
|
||||||
</Button>
|
|
||||||
</Group>
|
|
||||||
</Stack>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
{/* Modal para crear una biblioteca */}
|
|
||||||
<Modal
|
|
||||||
opened={modalNuevaBiblio}
|
|
||||||
onClose={() => setModalNuevaBiblio(false)}
|
|
||||||
title="Crear nueva biblioteca"
|
|
||||||
>
|
|
||||||
<Stack>
|
|
||||||
<TextInput
|
|
||||||
label="Nombre"
|
|
||||||
value={nombreBiblio}
|
|
||||||
onChange={(e) => setNombreBiblio(e.currentTarget.value)}
|
|
||||||
disabled={loadingNuevaBiblio}
|
|
||||||
/>
|
|
||||||
<TextInput
|
|
||||||
label="Descripción"
|
|
||||||
value={descripcionBiblio}
|
|
||||||
onChange={(e) => setDescripcionBiblio(e.currentTarget.value)}
|
|
||||||
disabled={loadingNuevaBiblio}
|
|
||||||
/>
|
|
||||||
<Button onClick={crearBiblioteca} loading={loadingNuevaBiblio}>
|
|
||||||
Crear
|
|
||||||
</Button>
|
|
||||||
</Stack>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
|
|
||||||
</AppShellWithMenu>
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
+1752
-18
File diff suppressed because it is too large
Load Diff
@@ -1,29 +0,0 @@
|
|||||||
# client.py
|
|
||||||
import asyncio
|
|
||||||
from src.Llms.MCPs.Mcp_client import MCPClient
|
|
||||||
from src.Llms.MCPs.Http_mcp_server import HttpMCPServer
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
client = MCPClient()
|
|
||||||
|
|
||||||
client.register_server(HttpMCPServer(
|
|
||||||
name="tools",
|
|
||||||
path="IGNORED_IN_CLIENT", # no importa aquí
|
|
||||||
host="127.0.0.1",
|
|
||||||
port=4300,
|
|
||||||
path_http="/tools"
|
|
||||||
))
|
|
||||||
|
|
||||||
await client.connect_all()
|
|
||||||
|
|
||||||
result = await client.call_tool({
|
|
||||||
"server": "tools",
|
|
||||||
"tool": "get_hostname",
|
|
||||||
"input": {}
|
|
||||||
})
|
|
||||||
print("RESULT:", result)
|
|
||||||
|
|
||||||
await client.disconnect_all()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
+90
-37
@@ -11,28 +11,71 @@ from fastmcp.client.transports import StreamableHttpTransport
|
|||||||
from fastmcp.client import Client
|
from fastmcp.client import Client
|
||||||
from src.Llms.MCPs.McpClient import MCPClient # ya tienes esta clase
|
from src.Llms.MCPs.McpClient import MCPClient # ya tienes esta clase
|
||||||
from src.Llms.MCPs.McpClient_Registry import ClientRegistry # o ajusta según tu estructura
|
from src.Llms.MCPs.McpClient_Registry import ClientRegistry # o ajusta según tu estructura
|
||||||
|
from src.Credenciales.ollama_credencial import OllamaCredencial
|
||||||
|
from src.ConexionApis.Ollama_cliente import OllamaCliente
|
||||||
|
from src.Llms.Modelos.Ollama_model import ModeloOllama
|
||||||
|
from src.Llms.MCPs.McpServer import MCPServerRunner
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
# Usar Credencial openai
|
# # Usar Credencial openai
|
||||||
|
|
||||||
conexion_admin = PostgresConexion(db_credencial)
|
conexion_admin = PostgresConexion(db_credencial)
|
||||||
repo = OpenAICredencialRepo(conexion_admin)
|
repo = OpenAICredencialRepo(conexion_admin)
|
||||||
credencial_openai = repo.get_by_id("OPAK20250513-61b29978b7604031014")
|
credencial_openai = repo.get_by_id("OPAK20250513-61b29978b7604031014")
|
||||||
|
if credencial_openai is None:
|
||||||
|
raise ValueError("No se encontró la credencial OpenAI con el ID proporcionado.")
|
||||||
cliente = OpenAICliente(credencial_openai)
|
cliente = OpenAICliente(credencial_openai)
|
||||||
|
|
||||||
|
|
||||||
|
# # Llamamos a los servidores para iniciarlos
|
||||||
|
|
||||||
# crea el modelo (openai)
|
# venv_python = r"E:\Fitz_Studio\.venv\Scripts\python.exe"
|
||||||
|
|
||||||
|
# # runner_math = MCPServerRunner(
|
||||||
|
# # r"E:\Fitz_Studio\src\Llms\MCPs\McpServers\server_math.py",
|
||||||
|
# # python_path=venv_python
|
||||||
|
# # )
|
||||||
|
# # runner_tools = MCPServerRunner(
|
||||||
|
# # r"E:\Fitz_Studio\src\Llms\MCPs\McpServers\server_utils.py",
|
||||||
|
# # python_path=venv_python
|
||||||
|
# # )
|
||||||
|
# runner_files = MCPServerRunner(
|
||||||
|
# r"E:\Fitz_Studio\src\Llms\MCPs\McpServers\server_files.py",
|
||||||
|
# python_path=venv_python
|
||||||
|
# )
|
||||||
|
|
||||||
|
|
||||||
|
# # await runner_math.start()
|
||||||
|
# # await runner_tools.start()
|
||||||
|
# await runner_files.start()
|
||||||
|
|
||||||
|
# # Esperamos un poco para asegurarnos de que los servidores estén listos
|
||||||
|
# await asyncio.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Usar Credencial ollama
|
||||||
|
# credencial_ollama = OllamaCredencial(titulo="Ollama")
|
||||||
|
|
||||||
|
# cliente = OllamaCliente(credencial_ollama)
|
||||||
|
|
||||||
|
# modelo = ModeloOllama(
|
||||||
|
# cliente=cliente,
|
||||||
|
# model="llama3.1:8b")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # crea el modelo (openai)
|
||||||
|
|
||||||
modelo = ModeloOpenAI(
|
modelo = ModeloOpenAI(
|
||||||
cliente=cliente,
|
cliente=cliente,
|
||||||
model="gpt-4o",
|
model="gpt-4o",
|
||||||
temperature=1,
|
temperature=1
|
||||||
top_p=1.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Le otorga memoria
|
# Le otorga memoria
|
||||||
@@ -46,45 +89,51 @@ async def main():
|
|||||||
|
|
||||||
# Cargamos las herramientas
|
# Cargamos las herramientas
|
||||||
|
|
||||||
herramientas = MCPClient.from_http(
|
# herramientas = MCPClient.from_http(
|
||||||
name="tools",
|
# name="tools",
|
||||||
url="http://127.0.0.1:4300/tools/"
|
# url="http://127.0.0.1:4300/tools/"
|
||||||
|
# )
|
||||||
|
|
||||||
|
# math = MCPClient.from_http(
|
||||||
|
# name="math",
|
||||||
|
# url="http://127.0.0.1:4200/math/"
|
||||||
|
# )
|
||||||
|
|
||||||
|
archivos = MCPClient.from_http(
|
||||||
|
name="files",
|
||||||
|
url="http://127.0.0.1:4201/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
math = MCPClient.from_http(
|
|
||||||
name="math",
|
|
||||||
url="http://127.0.0.1:4200/math/"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Las añadimos al registro de herramientas
|
# Las añadimos al registro de herramientas
|
||||||
|
|
||||||
registry = ClientRegistry()
|
registry = ClientRegistry()
|
||||||
|
|
||||||
|
|
||||||
registry.add("tools", herramientas)
|
# registry.add("tools", herramientas)
|
||||||
registry.add("math", math)
|
# registry.add("math", math)
|
||||||
|
registry.add("files", archivos)
|
||||||
|
|
||||||
|
|
||||||
# --- INICIALIZACIÓN DEL AGENTE ---
|
# --- INICIALIZACIÓN DEL AGENTE ---
|
||||||
agente2 = AgenteAI(
|
agente2 = AgenteAI(
|
||||||
modelo=modelo,
|
modelo=modelo,
|
||||||
nombre="Asistente Inteligente",
|
nombre="Asistente Inteligente",
|
||||||
descripcion="Un asistente conversacional versátil, capaz de resolver problemas, acceder a herramientas y proporcionar respuestas útiles.",
|
descripcion="",
|
||||||
system_prompt=(
|
system_prompt="",
|
||||||
"Eres un asistente inteligente que ayuda al usuario a resolver tareas, responder preguntas y usar herramientas disponibles si es necesario. "
|
# system_prompt = """
|
||||||
"Debes razonar paso a paso, y si se detecta que una herramienta MCP es útil, actúa generando el bloque MCP apropiado sin dar más explicaciones. "
|
# Eres un asistente general. No tienes acceso a herramientas externas ni herramientas MCP. No debes mencionar herramientas MCP, servidores ni bloques de código.
|
||||||
"Siempre estructura tus respuestas con claridad, y termina con <END> cuando creas haber completado la tarea."
|
# Responde de forma clara y amigable a cualquier pregunta general del usuario.
|
||||||
),
|
# """.strip(),
|
||||||
rol="asistente",
|
rol="asistente",
|
||||||
objetivos=[
|
objetivos=[
|
||||||
"Resolver tareas del usuario",
|
# "Resolver tareas del usuario",
|
||||||
"Usar herramientas MCP si es útil",
|
# "Usar herramientas MCP si es útil",
|
||||||
"Responder de forma clara y útil"
|
# "Responder de forma clara y útil"
|
||||||
],
|
],
|
||||||
|
|
||||||
# max_iterations=3,
|
max_iterations=0,
|
||||||
# memoria=memoria,
|
memoria=memoria,
|
||||||
|
|
||||||
mcp=registry # ← ✅ Integración del cliente MCP
|
mcp=registry # ← ✅ Integración del cliente MCP
|
||||||
)
|
)
|
||||||
@@ -92,26 +141,30 @@ async def main():
|
|||||||
|
|
||||||
# --- FUNCIÓN DE EJECUCIÓN ---
|
# --- FUNCIÓN DE EJECUCIÓN ---
|
||||||
async def probar_interaccion_stream():
|
async def probar_interaccion_stream():
|
||||||
# # 🔌 Conectar a los servidores MCP registrados
|
print("🧠 Agente iniciado. Escribe 'salir' para terminar.\n")
|
||||||
# await mcp_client.connect_all()
|
|
||||||
|
|
||||||
print("Respuesta en streaming:\n")
|
while True:
|
||||||
respuesta_gen = await agente2.interactuar_en_bucle(
|
prompt = input("\n📝 Escribe tu pregunta: ")
|
||||||
"¿Cuál es mi nombre de usuario en este sistema?",
|
if prompt.strip().lower() in ("salir", "exit", "quit"):
|
||||||
stream=True
|
print("\n👋 Hasta pronto.")
|
||||||
)
|
break
|
||||||
|
|
||||||
async for token in respuesta_gen:
|
print("\n💬 Respuesta en streaming:\n")
|
||||||
print(token, end="", flush=True)
|
respuesta_gen = await agente2.interactuar_en_bucle(
|
||||||
|
prompt=prompt,
|
||||||
|
stream=True
|
||||||
|
)
|
||||||
|
|
||||||
|
async for token in respuesta_gen:
|
||||||
|
print(token, end="", flush=True)
|
||||||
|
|
||||||
|
|
||||||
await probar_interaccion_stream()
|
await probar_interaccion_stream()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Ejecutar
|
# Ejecutar
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+23
-23
@@ -1,29 +1,29 @@
|
|||||||
|
import asyncio
|
||||||
|
from src.Llms.MCPs.McpServer import MCPServerRunner
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
venv_python = r"E:\Fitz_Studio\.venv\Scripts\python.exe"
|
||||||
|
|
||||||
|
runner_math = MCPServerRunner(
|
||||||
|
r"E:\Fitz_Studio\src\Llms\MCPs\McpServers\server_math.py",
|
||||||
|
python_path=venv_python
|
||||||
|
)
|
||||||
|
runner_tools = MCPServerRunner(
|
||||||
|
r"E:\Fitz_Studio\src\Llms\MCPs\McpServers\server_utils.py",
|
||||||
|
python_path=venv_python
|
||||||
|
)
|
||||||
|
|
||||||
|
await runner_math.start()
|
||||||
|
await runner_tools.start()
|
||||||
|
|
||||||
async def test_registry(registry: ClientRegistry):
|
try:
|
||||||
tools = await registry.listar_tools_por_cliente()
|
while True:
|
||||||
prompts = await registry.listar_prompts_por_cliente()
|
await asyncio.sleep(1)
|
||||||
resources = await registry.listar_resources_por_cliente()
|
except KeyboardInterrupt:
|
||||||
|
print("\n⛔ Terminando servidores...")
|
||||||
|
|
||||||
print("\n🔧 Herramientas:", tools)
|
await runner_math.stop()
|
||||||
|
await runner_tools.stop()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
print("\n📋 Prompts:", prompts)
|
asyncio.run(main())
|
||||||
|
|
||||||
print("\n📂 Resources:", resources)
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(test_registry(registry))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def test_wrapper():
|
|
||||||
|
|
||||||
# 2. Llamar a una herramienta de prueba
|
|
||||||
result = await herramientas.call_tool("generate_uuid")
|
|
||||||
print("\n🆔 UUID generado:", result[0].text) # Accedemos al contenido directamente
|
|
||||||
|
|
||||||
|
|
||||||
# asyncio.run(test_wrapper())
|
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
# Permitir acceso desde el frontend
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"], # cámbialo por ["http://localhost:5173"] si es necesario
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.get("/api/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"}]
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.get("/api/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"}]
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.get("/api/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"}
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.get("/api/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 @@
|
|||||||
|
Érase una vez un pequeño ratón que vivía en un bosque mágico. Un día, encontró una pequeña llave dorada...
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Había una vez un pequeño conejo que soñaba con saltar más alto que las nubes. Un día, encontró unas botas mágicas...
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
En un bosque encantado vivía una pequeña hada que siempre ayudaba a los animales a encontrar su camino de regreso a casa...
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Había una vez un osito que quería aprender a tocar la flauta mágica para alegrar a los habitantes del bosque...
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
En una colina lejana, vivía un conejo que podía correr tan rápido como el viento. Un día, decidió participar en la gran carrera del bosque...
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# main.py
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Bienvenido al sistema ERP')
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# test_sample.py
|
||||||
|
|
||||||
|
def test_placeholder():
|
||||||
|
assert True
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import binascii
|
||||||
|
import ctypes
|
||||||
|
import base64
|
||||||
|
import sqlite3
|
||||||
|
import pandas as pd
|
||||||
|
import pathlib
|
||||||
|
from Crypto.Cipher import AES, ChaCha20_Poly1305
|
||||||
|
from pypsexec.client import Client
|
||||||
|
|
||||||
|
"""
|
||||||
|
Este script extrae cookies v20 de Google Chrome y las guarda en un archivo CSV.
|
||||||
|
Requiere privilegios de administrador para acceder a los datos de Chrome.
|
||||||
|
|
||||||
|
Conseguido para poder extraer cookies de Chrome v20, que utiliza un nuevo formato de cifrado.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def is_admin():
|
||||||
|
try:
|
||||||
|
return ctypes.windll.shell32.IsUserAnAdmin() != 0
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_app_bound_key(local_state_path):
|
||||||
|
with open(local_state_path, "r", encoding="utf-8") as f:
|
||||||
|
local_state = json.load(f)
|
||||||
|
return local_state["os_crypt"]["app_bound_encrypted_key"]
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt_app_bound_key(encrypted_key_b64):
|
||||||
|
arguments = "-c \"" + """import win32crypt
|
||||||
|
import binascii
|
||||||
|
encrypted_key = win32crypt.CryptUnprotectData(binascii.a2b_base64('{}'), None, None, None, 0)
|
||||||
|
print(binascii.b2a_base64(encrypted_key[1]).decode())
|
||||||
|
""".replace("\n", ";") + "\""
|
||||||
|
|
||||||
|
c = Client("localhost")
|
||||||
|
c.connect()
|
||||||
|
|
||||||
|
decrypted_key = None
|
||||||
|
try:
|
||||||
|
c.create_service()
|
||||||
|
|
||||||
|
assert(binascii.a2b_base64(encrypted_key_b64)[:4] == b"APPB")
|
||||||
|
stripped_key_b64 = binascii.b2a_base64(binascii.a2b_base64(encrypted_key_b64)[4:]).decode().strip()
|
||||||
|
|
||||||
|
encrypted_key_b64_sys, _, _ = c.run_executable(
|
||||||
|
sys.executable,
|
||||||
|
arguments=arguments.format(stripped_key_b64),
|
||||||
|
use_system_account=True
|
||||||
|
)
|
||||||
|
|
||||||
|
decrypted_key_b64, _, _ = c.run_executable(
|
||||||
|
sys.executable,
|
||||||
|
arguments=arguments.format(encrypted_key_b64_sys.decode().strip()),
|
||||||
|
use_system_account=False
|
||||||
|
)
|
||||||
|
|
||||||
|
decrypted_key = binascii.a2b_base64(decrypted_key_b64)[-61:]
|
||||||
|
finally:
|
||||||
|
c.remove_service()
|
||||||
|
c.disconnect()
|
||||||
|
|
||||||
|
return decrypted_key
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt_final_key(encrypted_key):
|
||||||
|
aes_key = bytes.fromhex("B31C6E241AC846728DA9C1FAC4936651CFFB944D143AB816276BCC6DA0284787")
|
||||||
|
chacha20_key = bytes.fromhex("E98F37D7F4E1FA433D19304DC2258042090E2D1D7EEA7670D41F738D08729660")
|
||||||
|
|
||||||
|
flag = encrypted_key[0]
|
||||||
|
iv = encrypted_key[1:13]
|
||||||
|
ciphertext = encrypted_key[13:45]
|
||||||
|
tag = encrypted_key[45:]
|
||||||
|
|
||||||
|
if flag == 1:
|
||||||
|
cipher = AES.new(aes_key, AES.MODE_GCM, nonce=iv)
|
||||||
|
elif flag == 2:
|
||||||
|
cipher = ChaCha20_Poly1305.new(key=chacha20_key, nonce=iv)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unsupported flag: {flag}")
|
||||||
|
|
||||||
|
return cipher.decrypt_and_verify(ciphertext, tag)
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt_cookie_v20(encrypted_value, key):
|
||||||
|
cookie_iv = encrypted_value[3:15]
|
||||||
|
encrypted_cookie = encrypted_value[15:-16]
|
||||||
|
cookie_tag = encrypted_value[-16:]
|
||||||
|
cookie_cipher = AES.new(key, AES.MODE_GCM, nonce=cookie_iv)
|
||||||
|
decrypted_cookie = cookie_cipher.decrypt_and_verify(encrypted_cookie, cookie_tag)
|
||||||
|
return decrypted_cookie[32:].decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
def extract_all_v20_cookies():
|
||||||
|
user_profile = os.environ['USERPROFILE']
|
||||||
|
local_state_path = rf"{user_profile}\AppData\Local\Google\Chrome\User Data\Local State"
|
||||||
|
base_profile_path = rf"{user_profile}\AppData\Local\Google\Chrome\User Data"
|
||||||
|
|
||||||
|
app_bound_key_b64 = get_app_bound_key(local_state_path)
|
||||||
|
decrypted_key_raw = decrypt_app_bound_key(app_bound_key_b64)
|
||||||
|
final_key = decrypt_final_key(decrypted_key_raw)
|
||||||
|
|
||||||
|
perfiles_invalidos = {"System Profile", "Guest Profile", "CrashpadMetrics"}
|
||||||
|
perfiles = [
|
||||||
|
name for name in os.listdir(base_profile_path)
|
||||||
|
if os.path.isdir(os.path.join(base_profile_path, name))
|
||||||
|
and name not in perfiles_invalidos
|
||||||
|
and os.path.exists(os.path.join(base_profile_path, name, "Network", "Cookies"))
|
||||||
|
]
|
||||||
|
|
||||||
|
all_cookies = []
|
||||||
|
|
||||||
|
for profile in perfiles:
|
||||||
|
db_path = os.path.join(base_profile_path, profile, "Network", "Cookies")
|
||||||
|
con = sqlite3.connect(pathlib.Path(db_path).as_uri() + "?mode=ro", uri=True)
|
||||||
|
cur = con.cursor()
|
||||||
|
r = cur.execute("SELECT host_key, name, path, is_secure, is_httponly, expires_utc, last_access_utc, CAST(encrypted_value AS BLOB) from cookies;")
|
||||||
|
cookies = cur.fetchall()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
for row in cookies:
|
||||||
|
host, name, path, is_secure, is_httponly, expires_utc, last_access_utc, encrypted_value = row
|
||||||
|
encrypted_value_b64 = base64.b64encode(encrypted_value).decode()
|
||||||
|
|
||||||
|
if encrypted_value.startswith(b"v20"):
|
||||||
|
try:
|
||||||
|
value = decrypt_cookie_v20(encrypted_value, final_key)
|
||||||
|
print(f"[✓] {host} {name}: {value}")
|
||||||
|
all_cookies.append({
|
||||||
|
"host": host,
|
||||||
|
"name": name,
|
||||||
|
"path": path,
|
||||||
|
"value": value,
|
||||||
|
"encrypted_value_b64": encrypted_value_b64,
|
||||||
|
"expires_utc": expires_utc,
|
||||||
|
"is_secure": is_secure,
|
||||||
|
"is_httponly": is_httponly,
|
||||||
|
"last_access_utc": last_access_utc,
|
||||||
|
"profile": profile,
|
||||||
|
"is_decrypted": True,
|
||||||
|
"decrypt_error": ""
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[x] Error decrypting {host} {name}: {e}")
|
||||||
|
all_cookies.append({
|
||||||
|
"host": host,
|
||||||
|
"name": name,
|
||||||
|
"path": path,
|
||||||
|
"value": "",
|
||||||
|
"encrypted_value_b64": encrypted_value_b64,
|
||||||
|
"expires_utc": expires_utc,
|
||||||
|
"is_secure": is_secure,
|
||||||
|
"is_httponly": is_httponly,
|
||||||
|
"last_access_utc": last_access_utc,
|
||||||
|
"profile": profile,
|
||||||
|
"is_decrypted": False,
|
||||||
|
"decrypt_error": str(e)
|
||||||
|
})
|
||||||
|
|
||||||
|
return pd.DataFrame(all_cookies)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if not is_admin():
|
||||||
|
input("Este script necesita ejecutarse como administrador. Presiona Enter para reiniciar con privilegios...")
|
||||||
|
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join([sys.argv[0]] + sys.argv[1:]), None, 1)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
print("[*] Extrayendo cookies v20 desde todos los perfiles...")
|
||||||
|
df = extract_all_v20_cookies()
|
||||||
|
df.to_csv("cookies_extraidas.csv", index=False, encoding="utf-8")
|
||||||
|
print(f"[✓] Cookies v20 extraídas: {len(df)}")
|
||||||
|
print("[✓] Guardado en 'cookies_extraidas.csv'")
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import pyperclip
|
||||||
|
import re
|
||||||
|
from src.ScrappingWeb.Scrapper import Scrapper
|
||||||
|
|
||||||
|
def sanitizar(nombre: str) -> str:
|
||||||
|
return re.sub(r'[\\/*?:"<>|]', "_", nombre).strip()[:100]
|
||||||
|
|
||||||
|
OUTPUT_DIR = "esquemas_json"
|
||||||
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
ws_id = "F51AC05B27E1DEC4011E67369781596C"
|
||||||
|
ws_url = f"ws://127.0.0.1:9222/devtools/page/{ws_id}"
|
||||||
|
scrapper = Scrapper(debugging_url="http://127.0.0.1:9222")
|
||||||
|
|
||||||
|
print("🔌 Conectando a pestaña específica...")
|
||||||
|
|
||||||
|
tab = scrapper.get_tab(ws_url) or scrapper.get_tab(ws_id)
|
||||||
|
if not tab:
|
||||||
|
nuevas_tabs = await scrapper.obtener_tabs_existentes()
|
||||||
|
tab = next((t for t in nuevas_tabs if t.ws_url.rsplit("/", 1)[-1] == ws_id), None)
|
||||||
|
|
||||||
|
if not tab:
|
||||||
|
print("⚠️ La pestaña con ese ID no se encontró.")
|
||||||
|
return
|
||||||
|
|
||||||
|
elementos = await tab.get_elements_by_css_selector(
|
||||||
|
"#_0rif_bq-resource-tree > div.cfctest-tree-main.ng-tns-c3578326070-0 > ul > cfc-virtual-scroller > div > div.item-container > div > li"
|
||||||
|
)
|
||||||
|
|
||||||
|
for i, elemento in enumerate(elementos[:12]):
|
||||||
|
print(f"🖱️ Click #{i + 1}")
|
||||||
|
|
||||||
|
clickeable = await elemento.encontrar_hijo_clickeable()
|
||||||
|
if clickeable:
|
||||||
|
await clickeable.click()
|
||||||
|
else:
|
||||||
|
print(f"⚠️ No se encontró subelemento clickeable en #{i+1}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
texto_crudo = await elemento.obtener_texto()
|
||||||
|
nombre_archivo = sanitizar(texto_crudo or f"esquema_item_{i+1}")
|
||||||
|
print(f"📄 Nombre base del archivo: {nombre_archivo}.txt")
|
||||||
|
|
||||||
|
# ✅ Ejecutar JS en el navegador para simular flujo de copia
|
||||||
|
await tab.evaluar_js("""
|
||||||
|
(() => {
|
||||||
|
const boton = document.querySelector('button[id^="_0rif_bqui-table-copy-schema-btn"] span.mdc-button__label > span');
|
||||||
|
if (boton) boton.click();
|
||||||
|
})()
|
||||||
|
""")
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
await tab.evaluar_js("""
|
||||||
|
(() => {
|
||||||
|
const overlays = document.querySelectorAll("div.cdk-overlay-pane");
|
||||||
|
for (let overlay of overlays) {
|
||||||
|
const items = overlay.querySelectorAll("cfc-menu-item .cfc-menu-item-label");
|
||||||
|
for (let item of items) {
|
||||||
|
if (item.textContent.includes("Copiar como JSON")) {
|
||||||
|
item.click();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
""")
|
||||||
|
await asyncio.sleep(1.5)
|
||||||
|
|
||||||
|
try:
|
||||||
|
texto_json = pyperclip.paste()
|
||||||
|
file_path = os.path.join(OUTPUT_DIR, f"{nombre_archivo}.txt")
|
||||||
|
with open(file_path, "w", encoding="utf-8") as f:
|
||||||
|
f.write(texto_json)
|
||||||
|
print(f"✅ Guardado: {file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error al leer el portapapeles o guardar archivo: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import signal
|
||||||
|
|
||||||
|
def iniciar_chrome(chrome_path,
|
||||||
|
user_data_dir,
|
||||||
|
headless=False,
|
||||||
|
debugging_port=9222,
|
||||||
|
user_agent=None,
|
||||||
|
):
|
||||||
|
|
||||||
|
# Asegúrate de que el directorio del perfil exista
|
||||||
|
os.makedirs(user_data_dir, exist_ok=True)
|
||||||
|
|
||||||
|
# Lista de argumentos para Chrome
|
||||||
|
chrome_args = [
|
||||||
|
f"--remote-debugging-port={debugging_port}",
|
||||||
|
f"--user-data-dir={user_data_dir}",
|
||||||
|
"--disable-blink-features=AutomationControlled",
|
||||||
|
"--no-sandbox",
|
||||||
|
"--disable-web-security",
|
||||||
|
"--disable-extensions",
|
||||||
|
"--disable-dev-shm-usage",
|
||||||
|
"--disable-infobars",
|
||||||
|
"--disable-popup-blocking",
|
||||||
|
"--disable-default-apps",
|
||||||
|
"--mute-audio",
|
||||||
|
"--window-size=1024,1024",
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
if not headless:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
chrome_args.append("--headless=new") # para versiones recientes de Chrome
|
||||||
|
|
||||||
|
if not user_agent:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
chrome_args.append(f"--user-agent={user_agent}")
|
||||||
|
|
||||||
|
# Comando para iniciar Chrome
|
||||||
|
chrome_process = subprocess.Popen([chrome_path] + chrome_args)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print(f"Chrome iniciado (headless={headless}). Presiona Ctrl+C para salir.")
|
||||||
|
while True:
|
||||||
|
if chrome_process.poll() is not None:
|
||||||
|
print("Chrome se ha cerrado.")
|
||||||
|
break
|
||||||
|
time.sleep(1)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Terminando proceso de Chrome...")
|
||||||
|
chrome_process.terminate()
|
||||||
|
try:
|
||||||
|
chrome_process.wait(timeout=5)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
chrome_process.kill()
|
||||||
|
print("Chrome cerrado correctamente.")
|
||||||
|
|
||||||
|
|
||||||
|
# Ruta al ejecutable de Chrome
|
||||||
|
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
|
||||||
|
|
||||||
|
# Directorio para el perfil de usuario
|
||||||
|
user_data_dir = os.path.abspath("./Perfiles_usuario/chrome_profile")
|
||||||
|
|
||||||
|
# Puerto para la depuración remota
|
||||||
|
port = 9222
|
||||||
|
|
||||||
|
user_agent= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
|
||||||
|
|
||||||
|
|
||||||
|
# Llama a la función con True o False
|
||||||
|
iniciar_chrome(chrome_path=chrome_path,
|
||||||
|
user_data_dir=user_data_dir,
|
||||||
|
debugging_port=port,
|
||||||
|
headless=False,
|
||||||
|
user_agent=user_agent) # Cambia a True para modo headless
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from src.ScrappingWeb.Navegador import Navegador
|
||||||
|
from src.ScrappingWeb.Scrapper import Scrapper
|
||||||
|
from src.ScrappingWeb.Tab import Tab
|
||||||
|
import aiohttp
|
||||||
|
import csv
|
||||||
|
|
||||||
|
|
||||||
|
async def esperar_chrome_listo(port, timeout=10):
|
||||||
|
url = f"http://127.0.0.1:{port}/json"
|
||||||
|
for _ in range(timeout * 2):
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
raise TimeoutError(f"Chrome en puerto {port} no respondió dentro del tiempo esperado.")
|
||||||
|
|
||||||
|
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
|
||||||
|
|
||||||
|
def sanitizar_nombre(nombre: str) -> str:
|
||||||
|
# Eliminar caracteres inválidos para nombre de archivo
|
||||||
|
return re.sub(r'[\\/*?:"<>|]', "_", nombre).strip()[:100]
|
||||||
|
|
||||||
|
|
||||||
|
async def iniciar_y_scrapear(id: int):
|
||||||
|
user_data_dir = os.path.abspath(f"./Perfiles_usuario/chrome_profile_{id}")
|
||||||
|
port = 9222 + id
|
||||||
|
navegador = Navegador(
|
||||||
|
chrome_path=chrome_path,
|
||||||
|
user_data_dir=user_data_dir,
|
||||||
|
id=id,
|
||||||
|
download_dir=os.path.join(user_data_dir, "downloads"),
|
||||||
|
debugging_port=port,
|
||||||
|
headless=False,
|
||||||
|
user_agent=f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/{100+id}.0.0.0 Safari/537.36"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Iniciar navegador en background
|
||||||
|
asyncio.create_task(navegador.iniciar())
|
||||||
|
|
||||||
|
# Esperamos a que el navegador esté listo
|
||||||
|
await esperar_chrome_listo(port)
|
||||||
|
|
||||||
|
# Conectarse con el scraper al navegador
|
||||||
|
scrapper = Scrapper(debugging_url=f"http://127.0.0.1:{port}")
|
||||||
|
tab = await scrapper.nueva_tab("", wait_time=6)
|
||||||
|
|
||||||
|
# Ejecutar acciones desde la clase Tab
|
||||||
|
ua = await tab.obtener_user_agent()
|
||||||
|
print(f"🧭 [{id}] User-Agent:", ua)
|
||||||
|
|
||||||
|
title = await tab.evaluar_js("document.title")
|
||||||
|
print(f"📄 [{id}] Título:", title)
|
||||||
|
|
||||||
|
|
||||||
|
# botones= await tab.get_elements_by_css_selector("#mw-content-text > div.mw-content-ltr.mw-parser-output > figure:nth-child(27) > a > img")
|
||||||
|
|
||||||
|
# for boton in botones:
|
||||||
|
# await boton.click()
|
||||||
|
|
||||||
|
|
||||||
|
# # Crear carpeta si no existe
|
||||||
|
# os.makedirs("wikipedia_md", exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
# # Guardar el HTML completo
|
||||||
|
# html = await tab.obtener_html_completo()
|
||||||
|
# with open(f"contenido.html", "w", encoding="utf-8") as f:
|
||||||
|
# f.write(html)
|
||||||
|
|
||||||
|
# # Leer enlaces del CSV
|
||||||
|
# with open("enlaces_extraidos.csv", "r", encoding="utf-8") as f:
|
||||||
|
# reader = csv.reader(f)
|
||||||
|
# next(reader) # saltar encabezados
|
||||||
|
# enlaces = list(reader)
|
||||||
|
|
||||||
|
# for texto, enlace in enlaces:
|
||||||
|
# nombre_archivo = sanitizar_nombre(texto or "sin_titulo") + ".png"
|
||||||
|
# ruta_archivo = os.path.join("wikipedia", nombre_archivo)
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# print(f"🌐 Visitando: {enlace}")
|
||||||
|
# tab = await scrapper.nueva_tab(enlace, wait_time=6)
|
||||||
|
|
||||||
|
# await tab.capturar_screenshot(ruta_archivo)
|
||||||
|
# print(f"📸 Captura guardada: {ruta_archivo}")
|
||||||
|
|
||||||
|
# await tab.cerrar()
|
||||||
|
# except Exception as e:
|
||||||
|
# print(f"❌ Error con {enlace}: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
# await tab.capturar_screenshot(f"screenshot_{id}.png")
|
||||||
|
|
||||||
|
# html = await tab.obtener_html_completo()
|
||||||
|
# print(html)
|
||||||
|
|
||||||
|
# with open("contenido.html", "w", encoding="utf-8") as f:
|
||||||
|
# f.write(html)
|
||||||
|
|
||||||
|
# Extraer enlaces y guardarlos en CSV
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # # Cerrar tab y navegador si quieres
|
||||||
|
# await asyncio.sleep(10)
|
||||||
|
# await tab.cerrar()
|
||||||
|
# await navegador.cerrar()
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
tareas = [iniciar_y_scrapear(i) for i in range(1)]
|
||||||
|
await asyncio.gather(*tareas)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
from entrypoint.init_db import db_credencial
|
||||||
|
from src.Logger.logger_db import LoggerDB, logger
|
||||||
|
|
||||||
|
|
||||||
|
LoggerDB(db_credencial, "logger_eventos", created_by="sistema_agente")
|
||||||
|
|
||||||
|
logger.info("Esto solo se verá en la base de datos")
|
||||||
|
logger.error("No aparecerá en la terminal")
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import requests
|
||||||
|
from src.Credenciales.ollama_credencial import OllamaCredencial
|
||||||
|
|
||||||
|
class OllamaCliente:
|
||||||
|
def __init__(self, credencial: OllamaCredencial):
|
||||||
|
"""
|
||||||
|
Inicializa el cliente de Ollama con una instancia de OllamaCredencial.
|
||||||
|
"""
|
||||||
|
self.credencial = credencial
|
||||||
|
self.base_url = self.credencial.base_url
|
||||||
|
|
||||||
|
# --- Chat Completions ---
|
||||||
|
def chat_completion(self, model: str, messages: list, stream: bool = False, **kwargs):
|
||||||
|
url = f"{self.base_url}/api/chat"
|
||||||
|
payload = {
|
||||||
|
"model": model,
|
||||||
|
"messages": messages,
|
||||||
|
"stream": stream,
|
||||||
|
**kwargs
|
||||||
|
}
|
||||||
|
response = requests.post(url, json=payload, stream=stream)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
return self._handle_stream(response) if stream else response.json()
|
||||||
|
|
||||||
|
def _handle_stream(self, response):
|
||||||
|
for line in response.iter_lines():
|
||||||
|
if line:
|
||||||
|
try:
|
||||||
|
parsed = line.decode("utf-8")
|
||||||
|
# Extraer contenido si está en JSON como {"message":{"content":"..."},...}
|
||||||
|
if parsed.startswith("{"):
|
||||||
|
import json
|
||||||
|
data = json.loads(parsed)
|
||||||
|
if "message" in data and "content" in data["message"]:
|
||||||
|
yield data["message"]["content"]
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# --- Text Completion (legacy) ---
|
||||||
|
def completion(self, model: str, prompt: str, **kwargs):
|
||||||
|
url = f"{self.base_url}/api/generate"
|
||||||
|
payload = {
|
||||||
|
"model": model,
|
||||||
|
"prompt": prompt,
|
||||||
|
**kwargs
|
||||||
|
}
|
||||||
|
response = requests.post(url, json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
# --- Embeddings ---
|
||||||
|
def embedding(self, model: str, input: str | list[str], **kwargs):
|
||||||
|
url = f"{self.base_url}/api/embeddings"
|
||||||
|
payload = {
|
||||||
|
"model": model,
|
||||||
|
"prompt": input,
|
||||||
|
**kwargs
|
||||||
|
}
|
||||||
|
response = requests.post(url, json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||||
|
|
||||||
|
class OllamaCredencial:
|
||||||
|
def __init__(self, titulo: str, base_url: str = "http://localhost:11434", id: str = None):
|
||||||
|
"""
|
||||||
|
:param titulo: Nombre descriptivo para esta credencial de Ollama.
|
||||||
|
:param base_url: URL base donde está corriendo el servidor de Ollama (por defecto: localhost).
|
||||||
|
"""
|
||||||
|
self.id = id if id is not None else GeneradorIDUnico("OLLA").generar()
|
||||||
|
self.titulo = titulo
|
||||||
|
self.base_url = base_url.rstrip("/")
|
||||||
|
|
||||||
|
def get_headers(self) -> dict:
|
||||||
|
"""
|
||||||
|
Retorna encabezados para autenticación si se requiere en el futuro.
|
||||||
|
Por defecto, Ollama local no usa headers especiales.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
+290
-122
@@ -3,6 +3,14 @@ from src.Llms.Memory.Base_MemoryConv import MemoryConvABC
|
|||||||
from src.Llms.MCPs.McpClient_Registry import ClientRegistry
|
from src.Llms.MCPs.McpClient_Registry import ClientRegistry
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional, List, Union, AsyncGenerator
|
from typing import Optional, List, Union, AsyncGenerator
|
||||||
|
import re
|
||||||
|
import json
|
||||||
|
|
||||||
|
from entrypoint.init_db import db_credencial
|
||||||
|
from src.Logger.logger_db import LoggerDB, logger
|
||||||
|
LoggerDB(db_credencial, "logger_agentes", created_by="sistema")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AgenteAI:
|
class AgenteAI:
|
||||||
@@ -17,7 +25,7 @@ class AgenteAI:
|
|||||||
max_iterations: int = 1,
|
max_iterations: int = 1,
|
||||||
memoria: Optional[MemoryConvABC] = None,
|
memoria: Optional[MemoryConvABC] = None,
|
||||||
version: str = "1.0.0",
|
version: str = "1.0.0",
|
||||||
mcp: ClientRegistry = None,
|
mcp: Optional[ClientRegistry] = None,
|
||||||
output_schema: Optional[dict] = None,
|
output_schema: Optional[dict] = None,
|
||||||
):
|
):
|
||||||
self.modelo = modelo
|
self.modelo = modelo
|
||||||
@@ -48,219 +56,379 @@ class AgenteAI:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
async def generar_system_prompt(self) -> str:
|
||||||
async def full_system_prompt(self) -> str:
|
|
||||||
tools_str = await self._obtener_herramientas_disponibles_str()
|
|
||||||
return f"""
|
|
||||||
Eres un agente conversacional con acceso a herramientas MCP (Model Context Protocol).
|
|
||||||
|
|
||||||
Tu comportamiento sigue este flujo:
|
info = f"""Eres un agente de texto y te llamas {self.nombre}
|
||||||
|
|
||||||
1. **Piensa** para razonar tu decisión.
|
### Descripción:
|
||||||
2. **Decide** si:
|
{self.descripcion}
|
||||||
- puedes responder tú mismo,
|
|
||||||
- necesitas más información del usuario,
|
|
||||||
- o necesitas una herramienta MCP.
|
|
||||||
3. **Actúa**:
|
|
||||||
- Cuando uses MCP, termina **solo** con un bloque de código MCP y **nada más**.
|
|
||||||
- Ten en cuenta EXACTAMENTE los parámetros especificados.
|
|
||||||
- **No expliques, no hables después del bloque. Termina tu turno.**
|
|
||||||
|
|
||||||
---
|
### Rol:
|
||||||
|
{self.rol}
|
||||||
|
|
||||||
# Formato MCP
|
### Objetivos:
|
||||||
|
{chr(10).join(f"- {o}" for o in self.objetivos)}
|
||||||
|
|
||||||
```mcp
|
### System Prompt:
|
||||||
{{
|
{self.system_prompt}
|
||||||
"tool": "<nombre_de_la_herramienta>",
|
|
||||||
"input": {{
|
|
||||||
"clave": "valor"
|
|
||||||
}}
|
|
||||||
}}
|
|
||||||
Reglas clave:
|
|
||||||
|
|
||||||
Razonas antes de actuar.
|
Siempre estructura tus respuestas con claridad, y termina con <END> cuando hayas completado la tarea principal del usuario.
|
||||||
|
""".strip()
|
||||||
|
|
||||||
Nunca hables después de un bloque MCP.
|
return info
|
||||||
|
|
||||||
No combines respuestas y herramientas.
|
|
||||||
|
|
||||||
Piensa. Decide. Actúa.
|
|
||||||
|
|
||||||
Herramientas disponibles para usar con MCP:
|
|
||||||
{tools_str}
|
|
||||||
|
|
||||||
""".strip()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def construir_prompt_usuario(self, prompt_usuario: str) -> str:
|
||||||
|
bloques = []
|
||||||
|
|
||||||
# Conseguir las herramientas disponibles
|
if self.mcp:
|
||||||
|
tools_str = await self._obtener_herramientas_disponibles_str()
|
||||||
|
bloques.append(f"### Herramientas disponibles (MCP):\n{tools_str}")
|
||||||
|
bloques.append("""### Instrucciones para actuar con herramientas MCP:
|
||||||
|
Eres un agente conversacional con acceso a herramientas MCP. Cuando el usuario te haga una solicitud, sigue este proceso paso a paso:
|
||||||
|
---
|
||||||
|
🧠 **Piensa**:
|
||||||
|
Reflexiona en voz alta. Explica claramente qué crees que se necesita hacer y por qué.
|
||||||
|
🎯 **Decide**:
|
||||||
|
Elige si puedes resolverlo tú solo, si necesitas más información del usuario, o si una herramienta MCP sería útil.
|
||||||
|
⚙️ **Actúa**:
|
||||||
|
Si decides usar una herramienta, **escribe el bloque MCP justo después**, sin ningún texto extra después del bloque.
|
||||||
|
---
|
||||||
|
### Formato MCP:
|
||||||
|
|
||||||
|
```mcp
|
||||||
|
{
|
||||||
|
"server": "tools",
|
||||||
|
"tool": "get_current_user",
|
||||||
|
"input": {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### ❗ REGLAS IMPORTANTES:
|
||||||
|
|
||||||
|
- **Puedes pensar y decidir con texto normal**, pero:
|
||||||
|
- El **bloque MCP debe ser lo último** que aparece en tu mensaje.
|
||||||
|
- **NO escribas nada después del bloque MCP.**
|
||||||
|
- Solo usa `<END>` cuando:
|
||||||
|
- hayas terminado completamente la tarea del usuario,
|
||||||
|
- e interpretado la salida de las herramientas que usaste.
|
||||||
|
- Puedes hacer múltiples pasos si es necesario: usar una herramienta, esperar su salida, analizarla, usar otra, etc.
|
||||||
|
- Si decides no usar herramientas, simplemente responde como lo harías normalmente.
|
||||||
|
- Si no estás seguro de algo, **pide aclaraciones al usuario** antes de actuar.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
✅ Correcto:
|
||||||
|
```mcp
|
||||||
|
{
|
||||||
|
"server": "tools",
|
||||||
|
"tool": "generate_uuid",
|
||||||
|
"input": {}
|
||||||
|
}
|
||||||
|
````
|
||||||
|
🔵 Siempre usa ` ```mcp ` (con triple backtick y la palabra `mcp`) antes del JSON. No escribas nada después del bloque.
|
||||||
|
````
|
||||||
|
---
|
||||||
|
|
||||||
|
### ✅ Ejemplo correcto:
|
||||||
|
|
||||||
|
Necesito generar un identificador único para el usuario.
|
||||||
|
Para eso usaré la herramienta `generate_uuid` disponible.
|
||||||
|
|
||||||
|
```mcp
|
||||||
|
{
|
||||||
|
"server": "tools",
|
||||||
|
"tool": "generate_uuid",
|
||||||
|
"input": {}
|
||||||
|
}
|
||||||
|
|
||||||
|
""")
|
||||||
|
|
||||||
|
if self.memoria:
|
||||||
|
historial = self.memoria.cargar_historial_chat()
|
||||||
|
if historial:
|
||||||
|
memoria_str = "\n".join(
|
||||||
|
[f"{msg['role']}: {msg['content']}" for msg in historial]
|
||||||
|
)
|
||||||
|
bloques.append(f"### Memoria del chat:\n{memoria_str}")
|
||||||
|
|
||||||
|
if self.output_schema:
|
||||||
|
schema_str = str(self.output_schema)
|
||||||
|
bloques.append(f"### Salida esperada:\n{schema_str}")
|
||||||
|
|
||||||
|
bloques.append(f"### Prompt del usuario:\n{prompt_usuario}")
|
||||||
|
|
||||||
|
return "\n\n".join(bloques)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Conseguir las herramientas disponibles
|
||||||
|
|
||||||
async def _obtener_herramientas_disponibles_str(self) -> str:
|
async def _obtener_herramientas_disponibles_str(self) -> str:
|
||||||
|
logger.info("Inicio de obtención de herramientas disponibles")
|
||||||
|
|
||||||
if not self.mcp:
|
if not self.mcp:
|
||||||
|
logger.warning("No se ha definido el cliente MCP.")
|
||||||
return "No se han definido herramientas disponibles."
|
return "No se han definido herramientas disponibles."
|
||||||
|
|
||||||
herramientas = []
|
try:
|
||||||
tools_por_cliente = await self.mcp.listar_tools_por_cliente()
|
resultado = await self.mcp.listar_tools_por_cliente()
|
||||||
|
tools_por_cliente = resultado.get("tools", {})
|
||||||
|
errores = resultado.get("errores", {})
|
||||||
|
|
||||||
for name, tools in tools_por_cliente.items():
|
logger.debug(f"Tools obtenidas: {list(tools_por_cliente.keys())}")
|
||||||
if not tools:
|
logger.debug(f"Errores detectados: {list(errores.keys())}")
|
||||||
continue
|
|
||||||
herramientas.append(f"\n🔌 Cliente: {name}")
|
herramientas = []
|
||||||
for tool in tools:
|
|
||||||
props = tool.inputSchema.get("properties", {})
|
for name, tools in tools_por_cliente.items():
|
||||||
parametros = "\n ".join(f"- {k} ({v.get('type', '?')})" for k, v in props.items())
|
if not tools:
|
||||||
herramientas.append(f"""Nombre: {tool.name}
|
logger.info(f"Servidor {name} no tiene herramientas disponibles.")
|
||||||
Descripción: {tool.description}
|
continue
|
||||||
Parámetros:
|
|
||||||
{parametros}
|
herramientas.append(f"\n🔌 Server: {name}")
|
||||||
""")
|
for tool in tools:
|
||||||
return "\n".join(herramientas) or "No hay herramientas disponibles actualmente."
|
props = tool.inputSchema.get("properties", {})
|
||||||
|
parametros = "\n ".join(f"- {k} ({v.get('type', '?')})" for k, v in props.items())
|
||||||
|
herramientas.append(f"""Nombre: {tool.name}
|
||||||
|
Descripción: {tool.description}
|
||||||
|
Parámetros:
|
||||||
|
{parametros}
|
||||||
|
""")
|
||||||
|
logger.debug(f"Herramienta agregada: {tool.name} del servidor {name}")
|
||||||
|
|
||||||
|
if errores:
|
||||||
|
herramientas.append("\n⚠️ Los siguientes servidores no están disponibles:")
|
||||||
|
for name, error in errores.items():
|
||||||
|
herramientas.append(f"- {name}: {error}")
|
||||||
|
logger.warning(f"Servidor con error: {name} -> {error}")
|
||||||
|
|
||||||
|
logger.info("Finalización de obtención de herramientas exitosamente.")
|
||||||
|
return "\n".join(herramientas) or "No hay herramientas disponibles actualmente."
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error inesperado al obtener herramientas: {str(e)}", exc_info=True)
|
||||||
|
return "Se produjo un error al obtener las herramientas disponibles."
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Formatear prompt para agentes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Formatear prompt para agentes
|
|
||||||
|
|
||||||
def _formatear_prompt(self, mensajes: List[dict]) -> str:
|
def _formatear_prompt(self, mensajes: List[dict]) -> str:
|
||||||
return "\n".join([f"{msg['role']}: {msg['content']}" for msg in mensajes])
|
return "\n".join([f"{msg['role']}: {msg['content']}" for msg in mensajes])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Ejecutar codigo MCP
|
||||||
|
|
||||||
|
async def ejecutar_bloque_mcp(self, respuesta: str) -> Optional[str]:
|
||||||
|
logger.info("Iniciando ejecución de bloque MCP.")
|
||||||
|
|
||||||
|
patron = r"```mcp\s*(\{.*?\})\s*```"
|
||||||
|
match = re.search(patron, respuesta, re.DOTALL)
|
||||||
|
|
||||||
|
if not match:
|
||||||
|
patron_incorrecto = r"```[\s]*\{.*?\}[\s]*```"
|
||||||
|
if re.search(patron_incorrecto, respuesta, re.DOTALL):
|
||||||
|
logger.warning("Bloque detectado sin especificador `mcp`.")
|
||||||
|
return "Advertencia: Usaste un bloque de herramienta MCP pero olvidaste indicar el lenguaje `mcp`. Corrige el bloque a: ```mcp { ... } ```"
|
||||||
|
logger.info("No se encontró ningún bloque MCP en la respuesta.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
bloque_json_str = match.group(1)
|
||||||
|
logger.debug(f"Bloque MCP detectado: {bloque_json_str}")
|
||||||
|
|
||||||
|
bloque = json.loads(bloque_json_str)
|
||||||
|
|
||||||
|
server_name = bloque["server"]
|
||||||
|
tool_name = bloque["tool"]
|
||||||
|
input_args = bloque.get("input", {})
|
||||||
|
|
||||||
|
logger.info(f"Bloque MCP válido. Servidor: {server_name}, Herramienta: {tool_name}")
|
||||||
|
logger.debug(f"Parámetros de entrada: {input_args}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error al interpretar el bloque MCP: {e}", exc_info=True)
|
||||||
|
return f"Error al interpretar el bloque MCP: {e}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
cliente_mcp = self.mcp.get(server_name)
|
||||||
|
except KeyError:
|
||||||
|
logger.warning(f"No se encontró el cliente MCP para el servidor '{server_name}'.")
|
||||||
|
return f"No se encontró el cliente MCP para el servidor '{server_name}'"
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.info(f"Ejecutando herramienta '{tool_name}' en servidor '{server_name}' con argumentos: {json.dumps(input_args, ensure_ascii=False)}")
|
||||||
|
|
||||||
|
async with cliente_mcp:
|
||||||
|
resultado = await cliente_mcp.call_tool(tool_name, input_args)
|
||||||
|
logger.info(f"Ejecución completada exitosamente. Resultado: {resultado}")
|
||||||
|
return str(resultado)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error al ejecutar herramienta '{tool_name}' en servidor '{server_name}': {e}", exc_info=True)
|
||||||
|
return f"Error al ejecutar herramienta '{tool_name}' en servidor '{server_name}': {e}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Ejecutar VARIOS bloques MCP
|
||||||
|
|
||||||
|
async def ejecutar_multiples_bloques_mcp(self, respuesta: str) -> Optional[List[str]]:
|
||||||
|
logger.info("Buscando múltiples bloques MCP en la respuesta.")
|
||||||
|
|
||||||
|
patron = r"```mcp\s*(\{.*?\})\s*```"
|
||||||
|
matches = re.finditer(patron, respuesta, re.DOTALL)
|
||||||
|
|
||||||
|
resultados = []
|
||||||
|
hubo_bloques = False
|
||||||
|
|
||||||
|
for match in matches:
|
||||||
|
hubo_bloques = True
|
||||||
|
bloque_json_str = match.group(1)
|
||||||
|
try:
|
||||||
|
bloque = json.loads(bloque_json_str)
|
||||||
|
server_name = bloque["server"]
|
||||||
|
tool_name = bloque["tool"]
|
||||||
|
input_args = bloque.get("input", {})
|
||||||
|
|
||||||
|
logger.info(f"Ejecutando bloque MCP: servidor={server_name}, herramienta={tool_name}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
cliente_mcp = self.mcp.get(server_name)
|
||||||
|
except KeyError:
|
||||||
|
msg = f"No se encontró el cliente MCP para el servidor '{server_name}'"
|
||||||
|
logger.warning(msg)
|
||||||
|
resultados.append(msg)
|
||||||
|
continue
|
||||||
|
|
||||||
|
async with cliente_mcp:
|
||||||
|
resultado = await cliente_mcp.call_tool(tool_name, input_args)
|
||||||
|
resultado_str = f"[{server_name}.{tool_name}] → {resultado}"
|
||||||
|
resultados.append(resultado_str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Error al procesar bloque MCP: {str(e)}"
|
||||||
|
logger.error(error_msg, exc_info=True)
|
||||||
|
resultados.append(error_msg)
|
||||||
|
|
||||||
|
if not hubo_bloques:
|
||||||
|
logger.info("No se encontró ningún bloque MCP en la respuesta.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return resultados
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
###----------- Funcion para interactuar
|
||||||
|
|
||||||
###----------- Funcion para interactuar
|
|
||||||
|
|
||||||
async def interactuar(self, prompt: str, stream: bool = False) -> Union[str, AsyncGenerator[str, None]]:
|
async def interactuar(self, prompt: str, stream: bool = False) -> Union[str, AsyncGenerator[str, None]]:
|
||||||
historial = self.memoria.cargar_historial_chat() if self.memoria else []
|
mensaje_usuario = await self.construir_prompt_usuario(prompt)
|
||||||
contexto = historial + [{"role": "user", "content": prompt}]
|
contexto = [{"role": "user", "content": mensaje_usuario}]
|
||||||
prompt_final = self._formatear_prompt(contexto)
|
prompt_final = self._formatear_prompt(contexto)
|
||||||
|
|
||||||
respuesta = await self.modelo.responder(
|
respuesta = await self.modelo.responder(
|
||||||
prompt=prompt_final,
|
prompt=prompt_final,
|
||||||
system_prompt=await self.full_system_prompt, # ✅ correcto
|
system_prompt=await self.generar_system_prompt(),
|
||||||
stream=stream
|
stream=stream
|
||||||
)
|
)
|
||||||
|
|
||||||
if stream:
|
return respuesta
|
||||||
async def wrapper():
|
|
||||||
buffer_respuesta = ""
|
|
||||||
async for token in respuesta:
|
|
||||||
buffer_respuesta += token
|
|
||||||
yield token
|
|
||||||
if self.memoria:
|
|
||||||
self.memoria.guardar_turno("user", prompt)
|
|
||||||
self.memoria.guardar_turno("assistant", buffer_respuesta)
|
|
||||||
self.numero_interacciones += 1
|
|
||||||
self.updated_at = datetime.now()
|
|
||||||
return wrapper()
|
|
||||||
else:
|
|
||||||
if self.memoria:
|
|
||||||
self.memoria.guardar_turno("user", prompt)
|
|
||||||
self.memoria.guardar_turno("assistant", respuesta)
|
|
||||||
self.numero_interacciones += 1
|
|
||||||
self.updated_at = datetime.now()
|
|
||||||
return respuesta
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
###----------- Funcion para interactuar en bucle
|
||||||
|
|
||||||
###----------- Funcion para interactuar en bucle
|
|
||||||
|
|
||||||
async def interactuar_en_bucle(self, prompt: str, stream: bool = False) -> Union[List[str], AsyncGenerator[str, None]]:
|
async def interactuar_en_bucle(self, prompt: str, stream: bool = False) -> Union[List[str], AsyncGenerator[str, None]]:
|
||||||
print("🚀 [interactuar_en_bucle] Iniciando interacción")
|
|
||||||
historial = self.memoria.cargar_historial_chat() if self.memoria else []
|
|
||||||
print(f"📜 [interactuar_en_bucle] Historial cargado: {historial}")
|
|
||||||
respuestas = [] if not stream else None
|
respuestas = [] if not stream else None
|
||||||
respuesta_anterior = None
|
respuesta_anterior = ""
|
||||||
|
resultado_mcp_anterior = None # <-- Guarda último resultado del MCP
|
||||||
iteration = 0
|
iteration = 0
|
||||||
prompt_original = prompt.strip()
|
prompt_original = prompt.strip()
|
||||||
print(f"✏️ [interactuar_en_bucle] Prompt original: {prompt_original}")
|
|
||||||
|
|
||||||
async def generador():
|
async def generador():
|
||||||
nonlocal iteration, respuesta_anterior
|
nonlocal iteration, respuesta_anterior, resultado_mcp_anterior
|
||||||
prompt_actual = prompt_original
|
|
||||||
|
|
||||||
while self.max_iterations == 0 or iteration < self.max_iterations:
|
while self.max_iterations == 0 or iteration < self.max_iterations:
|
||||||
print(f"\n🔁 [generador] Iteración: {iteration}")
|
instruccion_fin = (
|
||||||
|
"\n\nIMPORTANTE: Cuando hayas respondido completamente a la pregunta original del usuario y no requieras más pasos, "
|
||||||
|
"escribe <END> para indicar que has terminado."
|
||||||
|
)
|
||||||
|
|
||||||
if iteration == 0:
|
if iteration == 0:
|
||||||
prompt_actual += (
|
prompt_actual = prompt_original + instruccion_fin
|
||||||
"\n\nIMPORTANTE:\n"
|
|
||||||
"Si al revisar tu última respuesta y mi pregunta inicial consideras que has terminado, "
|
|
||||||
"di alguna de estas frases: <END>"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
prompt_actual = (
|
prompt_actual = (
|
||||||
f"Esta es la pregunta original:\n{prompt_original}\n\n"
|
f"Esta es la pregunta original:\n{prompt_original}\n\n"
|
||||||
f"Esto fue lo último que dijiste:\n{respuesta_anterior}\n"
|
f"Esto fue lo último que dijiste:\n{respuesta_anterior}\n\n"
|
||||||
"\n\nIMPORTANTE:\n"
|
f"{instruccion_fin}"
|
||||||
"Si al revisar tu última respuesta y mi pregunta inicial consideras que has terminado, "
|
|
||||||
"di alguna de estas frases: <END>"
|
|
||||||
"En caso contrario, responde a la pregunta original "
|
|
||||||
"y añade información relevante que no hayas mencionado antes.\n\n"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
contexto = historial + [{"role": "user", "content": prompt_actual}]
|
if resultado_mcp_anterior:
|
||||||
|
prompt_actual += (
|
||||||
|
"\n\nEsta fue la salida de la herramienta que usaste:\n"
|
||||||
|
f"{resultado_mcp_anterior}\n\n"
|
||||||
|
"Úsala para seguir resolviendo el problema o tomar una nueva decisión."
|
||||||
|
)
|
||||||
|
|
||||||
|
mensaje_usuario = await self.construir_prompt_usuario(prompt_actual)
|
||||||
|
contexto = [{"role": "user", "content": mensaje_usuario}]
|
||||||
prompt_final = self._formatear_prompt(contexto)
|
prompt_final = self._formatear_prompt(contexto)
|
||||||
|
|
||||||
print(f"📨 [generador] Prompt final enviado al modelo:\n{prompt_final}")
|
|
||||||
|
|
||||||
print("🤖 [generador] Esperando respuesta del modelo...")
|
|
||||||
respuesta = await self.modelo.responder(
|
respuesta = await self.modelo.responder(
|
||||||
prompt=prompt_final,
|
prompt=prompt_final,
|
||||||
system_prompt=await self.full_system_prompt,
|
system_prompt=await self.generar_system_prompt(),
|
||||||
stream=stream
|
stream=stream
|
||||||
)
|
)
|
||||||
print("✅ [generador] Respuesta recibida")
|
|
||||||
|
|
||||||
if stream:
|
if stream:
|
||||||
buffer_respuesta = ""
|
buffer_respuesta = ""
|
||||||
async for token in respuesta:
|
async for token in respuesta:
|
||||||
buffer_respuesta += token
|
buffer_respuesta += token
|
||||||
# print(f"🔹 [stream] Token: {token}")
|
|
||||||
yield token
|
yield token
|
||||||
respuesta_anterior = buffer_respuesta
|
respuesta_anterior = buffer_respuesta
|
||||||
# print(f"📦 [stream] Respuesta completa:\n{respuesta_anterior}")
|
|
||||||
else:
|
else:
|
||||||
respuestas.append(respuesta)
|
respuestas.append(respuesta)
|
||||||
respuesta_anterior = respuesta
|
respuesta_anterior = respuesta
|
||||||
# print(f"📦 [generador] Respuesta completa:\n{respuesta_anterior}")
|
|
||||||
|
|
||||||
|
# Revisar y ejecutar bloque MCP si existe
|
||||||
|
resultado_mcp_anterior = None
|
||||||
|
if "```mcp" in respuesta_anterior:
|
||||||
|
resultados_mcp = await self.ejecutar_multiples_bloques_mcp(respuesta_anterior)
|
||||||
|
if resultados_mcp:
|
||||||
|
resultado_mcp_anterior = "\n".join(resultados_mcp)
|
||||||
|
|
||||||
|
if stream:
|
||||||
|
yield "\n" + resultado_mcp_anterior
|
||||||
|
else:
|
||||||
|
respuestas.append(resultado_mcp_anterior)
|
||||||
|
|
||||||
|
# Guardar historial si hay memoria
|
||||||
if self.memoria:
|
if self.memoria:
|
||||||
print("💾 [memoria] Guardando turno en la memoria...")
|
|
||||||
self.memoria.guardar_turno("user", prompt_actual)
|
self.memoria.guardar_turno("user", prompt_actual)
|
||||||
self.memoria.guardar_turno("assistant", respuesta_anterior)
|
self.memoria.guardar_turno("assistant", respuesta_anterior)
|
||||||
|
|
||||||
self.numero_interacciones += 1
|
self.numero_interacciones += 1
|
||||||
self.updated_at = datetime.now()
|
self.updated_at = datetime.now()
|
||||||
print(f"📊 [generador] Interacción #{self.numero_interacciones} registrada")
|
|
||||||
|
|
||||||
if "<end>" in respuesta_anterior.lower():
|
if "<end>" in respuesta_anterior.lower() and "```mcp" not in respuesta_anterior.lower():
|
||||||
print("🛑 [generador] Detectado <end>. Terminando bucle.")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
iteration += 1
|
iteration += 1
|
||||||
prompt_actual = ""
|
|
||||||
|
|
||||||
return generador() if stream else await generador_to_list(generador)
|
return generador() if stream else await generador_to_list(generador())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from fastmcp.client.transports import (
|
|||||||
)
|
)
|
||||||
from mcp.types import *
|
from mcp.types import *
|
||||||
from fastmcp.exceptions import ClientError
|
from fastmcp.exceptions import ClientError
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
class MCPClient:
|
class MCPClient:
|
||||||
@@ -52,10 +53,13 @@ class MCPClient:
|
|||||||
|
|
||||||
# Delegación MCP
|
# Delegación MCP
|
||||||
|
|
||||||
async def call_tool(
|
async def call_tool(self, name: str, arguments: dict[str, Any] | None = None) -> list[TextContent | ImageContent | EmbeddedResource]:
|
||||||
self, name: str, arguments: dict[str, Any] | None = None
|
try:
|
||||||
) -> list[TextContent | ImageContent | EmbeddedResource]:
|
return await asyncio.wait_for(
|
||||||
return await self.client.call_tool(name, arguments)
|
self.client.call_tool(name, arguments), timeout=10
|
||||||
|
)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
raise RuntimeError(f"Timeout al ejecutar herramienta '{name}'")
|
||||||
|
|
||||||
async def get_prompt(
|
async def get_prompt(
|
||||||
self, name: str, arguments: dict[str, str] | None = None
|
self, name: str, arguments: dict[str, str] | None = None
|
||||||
|
|||||||
@@ -22,35 +22,35 @@ class ClientRegistry:
|
|||||||
def __contains__(self, name: str) -> bool:
|
def __contains__(self, name: str) -> bool:
|
||||||
return name in self._clients
|
return name in self._clients
|
||||||
|
|
||||||
async def listar_tools_por_cliente(self) -> dict[str, list[Any]]:
|
async def listar_tools_por_cliente(self) -> dict[str, Any]:
|
||||||
resultado = {}
|
resultado = {"tools": {}, "errores": {}}
|
||||||
for name, wrapper in self._clients.items():
|
for name, wrapper in self._clients.items():
|
||||||
try:
|
try:
|
||||||
async with wrapper:
|
async with wrapper:
|
||||||
resultado[name] = await wrapper.list_tools()
|
resultado["tools"][name] = await wrapper.list_tools()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[TOOLS] ❌ Error en '{name}': {e}")
|
resultado["errores"][name] = str(e)
|
||||||
resultado[name] = []
|
resultado["tools"][name] = []
|
||||||
return resultado
|
return resultado
|
||||||
|
|
||||||
async def listar_prompts_por_cliente(self) -> dict[str, list[Any]]:
|
async def listar_prompts_por_cliente(self) -> dict[str, Any]:
|
||||||
resultado = {}
|
resultado = {"prompts": {}, "errores": {}}
|
||||||
for name, wrapper in self._clients.items():
|
for name, wrapper in self._clients.items():
|
||||||
try:
|
try:
|
||||||
async with wrapper:
|
async with wrapper:
|
||||||
resultado[name] = await wrapper.list_prompts()
|
resultado["prompts"][name] = await wrapper.list_prompts()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[PROMPTS] ❌ Error en '{name}': {e}")
|
resultado["errores"][name] = str(e)
|
||||||
resultado[name] = []
|
resultado["prompts"][name] = []
|
||||||
return resultado
|
return resultado
|
||||||
|
|
||||||
async def listar_resources_por_cliente(self) -> dict[str, list[Any]]:
|
async def listar_resources_por_cliente(self) -> dict[str, Any]:
|
||||||
resultado = {}
|
resultado = {"resources": {}, "errores": {}}
|
||||||
for name, wrapper in self._clients.items():
|
for name, wrapper in self._clients.items():
|
||||||
try:
|
try:
|
||||||
async with wrapper:
|
async with wrapper:
|
||||||
resultado[name] = await wrapper.list_resources()
|
resultado["resources"][name] = await wrapper.list_resources()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[RESOURCES] ❌ Error en '{name}': {e}")
|
resultado["errores"][name] = str(e)
|
||||||
resultado[name] = []
|
resultado["resources"][name] = []
|
||||||
return resultado
|
return resultado
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# server_runner.py
|
||||||
|
import subprocess
|
||||||
|
import asyncio
|
||||||
|
import socket
|
||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
async def wait_for_port(host: str, port: int, timeout: float = 10.0):
|
||||||
|
for _ in range(int(timeout * 10)):
|
||||||
|
try:
|
||||||
|
with socket.create_connection((host, port), timeout=0.5):
|
||||||
|
return True
|
||||||
|
except (OSError, ConnectionRefusedError):
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
raise TimeoutError(f"No se pudo conectar al servidor en {host}:{port}")
|
||||||
|
|
||||||
|
class MCPServerRunner:
|
||||||
|
def __init__(self, server_script_path: str, python_path: str = "python"):
|
||||||
|
self.server_script_path = server_script_path
|
||||||
|
self.python_path = python_path
|
||||||
|
self.port: int = self._extraer_puerto()
|
||||||
|
self.process: subprocess.Popen | None = None
|
||||||
|
|
||||||
|
def _extraer_puerto(self) -> int:
|
||||||
|
contenido = Path(self.server_script_path).read_text(encoding="utf-8")
|
||||||
|
coincidencias = re.findall(r"port\s*=\s*(\d+)", contenido)
|
||||||
|
if not coincidencias:
|
||||||
|
raise ValueError(f"No se pudo detectar el puerto en {self.server_script_path}")
|
||||||
|
return int(coincidencias[0])
|
||||||
|
|
||||||
|
async def start(self):
|
||||||
|
if self.process is None or self.process.poll() is not None:
|
||||||
|
self.process = subprocess.Popen(
|
||||||
|
[self.python_path, self.server_script_path],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE
|
||||||
|
)
|
||||||
|
await wait_for_port("127.0.0.1", self.port)
|
||||||
|
print(f"🟢 Servidor MCP iniciado en puerto {self.port}")
|
||||||
|
|
||||||
|
async def stop(self):
|
||||||
|
if self.process and self.process.poll() is None:
|
||||||
|
self.process.terminate()
|
||||||
|
try:
|
||||||
|
self.process.wait(timeout=5)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
self.process.kill()
|
||||||
|
print("🔴 Servidor MCP detenido")
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
from fastmcp import FastMCP
|
||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
# Directorio base seguro
|
||||||
|
SANDBOX_DIR = Path("./sandbox").resolve()
|
||||||
|
SANDBOX_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
def safe_path(requested_path: str) -> Path:
|
||||||
|
"""Siempre interpreta la ruta como relativa al SANDBOX_DIR, incluso si empieza con '/'."""
|
||||||
|
# Normaliza la ruta quitando el primer '/'
|
||||||
|
normalized = requested_path.strip().lstrip("/")
|
||||||
|
full_path = (SANDBOX_DIR / normalized).resolve()
|
||||||
|
|
||||||
|
if not full_path.is_relative_to(SANDBOX_DIR):
|
||||||
|
raise ValueError("Ruta fuera del directorio permitido.")
|
||||||
|
return full_path
|
||||||
|
|
||||||
|
mcp = FastMCP()
|
||||||
|
|
||||||
|
@mcp.tool(description="Lee y devuelve el contenido de un archivo de texto ubicado en el sistema de archivos seguro. El archivo debe estar dentro del sandbox.")
|
||||||
|
def read_file(path: str) -> str:
|
||||||
|
try:
|
||||||
|
file_path = safe_path(path)
|
||||||
|
if not file_path.is_file():
|
||||||
|
raise FileNotFoundError(f"Archivo '{path}' no encontrado.")
|
||||||
|
return file_path.read_text(encoding="utf-8")
|
||||||
|
except Exception as e:
|
||||||
|
return f"⚠️ Error al leer archivo '{path}': {str(e)}"
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool(description="Escribe contenido de texto en un archivo dentro del sandbox. Si el archivo ya existe, será sobrescrito.")
|
||||||
|
def write_file(path: str, content: str) -> str:
|
||||||
|
file_path = safe_path(path)
|
||||||
|
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
file_path.write_text(content, encoding="utf-8")
|
||||||
|
return "Archivo guardado correctamente."
|
||||||
|
|
||||||
|
@mcp.tool(description="Elimina de forma segura un archivo ubicado dentro del sandbox.")
|
||||||
|
def delete_file(path: str) -> str:
|
||||||
|
file_path = safe_path(path)
|
||||||
|
if not file_path.is_file():
|
||||||
|
raise FileNotFoundError("Archivo no encontrado.")
|
||||||
|
file_path.unlink()
|
||||||
|
return "Archivo eliminado."
|
||||||
|
|
||||||
|
@mcp.tool(description="Crea una carpeta (y sus carpetas padre si es necesario) dentro del sandbox.")
|
||||||
|
def create_folder(path: str) -> str:
|
||||||
|
folder_path = safe_path(path)
|
||||||
|
folder_path.mkdir(parents=True, exist_ok=True)
|
||||||
|
return "Carpeta creada."
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool(description="Lista archivos y carpetas dentro de una ruta del sandbox.")
|
||||||
|
def list_directory(path: str = ".") -> list[str]:
|
||||||
|
folder = safe_path(path)
|
||||||
|
if not folder.is_dir():
|
||||||
|
raise NotADirectoryError("Ruta no corresponde a una carpeta.")
|
||||||
|
return sorted(str(p.relative_to(SANDBOX_DIR)) for p in folder.iterdir())
|
||||||
|
|
||||||
|
@mcp.tool(description="Muestra la estructura de carpetas y archivos como un árbol, desde una ruta dentro del sandbox.")
|
||||||
|
def tree(path: str = ".", depth: int = 3) -> str:
|
||||||
|
base = safe_path(path)
|
||||||
|
if not base.is_dir():
|
||||||
|
raise NotADirectoryError("Ruta no corresponde a una carpeta.")
|
||||||
|
|
||||||
|
tree_output = []
|
||||||
|
|
||||||
|
def walk(dir_path: Path, prefix: str = "", level: int = 0):
|
||||||
|
if level > depth:
|
||||||
|
return
|
||||||
|
entries = sorted(dir_path.iterdir())
|
||||||
|
for i, entry in enumerate(entries):
|
||||||
|
connector = "└── " if i == len(entries) - 1 else "├── "
|
||||||
|
tree_output.append(f"{prefix}{connector}{entry.name}")
|
||||||
|
if entry.is_dir():
|
||||||
|
extension = " " if i == len(entries) - 1 else "│ "
|
||||||
|
walk(entry, prefix + extension, level + 1)
|
||||||
|
|
||||||
|
tree_output.append(f"{base.name}/")
|
||||||
|
walk(base)
|
||||||
|
return "\n".join(tree_output)
|
||||||
|
|
||||||
|
@mcp.tool(description="Devuelve información detallada sobre un archivo: tamaño en bytes, fecha de modificación y tipo.")
|
||||||
|
def file_info(path: str) -> dict:
|
||||||
|
fpath = safe_path(path)
|
||||||
|
if not fpath.exists():
|
||||||
|
raise FileNotFoundError("Archivo no encontrado.")
|
||||||
|
return {
|
||||||
|
"nombre": fpath.name,
|
||||||
|
"tipo": "carpeta" if fpath.is_dir() else "archivo",
|
||||||
|
"tamaño_bytes": fpath.stat().st_size,
|
||||||
|
"última_modificación": datetime.fromtimestamp(fpath.stat().st_mtime).isoformat(),
|
||||||
|
"relativo": str(fpath.relative_to(SANDBOX_DIR))
|
||||||
|
}
|
||||||
|
|
||||||
|
@mcp.tool(description="Copia un archivo o carpeta dentro del sandbox a otra ruta.")
|
||||||
|
def copy_file(src: str, dest: str) -> str:
|
||||||
|
src_path = safe_path(src)
|
||||||
|
dest_path = safe_path(dest)
|
||||||
|
if src_path.is_dir():
|
||||||
|
shutil.copytree(src_path, dest_path, dirs_exist_ok=True)
|
||||||
|
else:
|
||||||
|
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
shutil.copy2(src_path, dest_path)
|
||||||
|
return "Copia completada."
|
||||||
|
|
||||||
|
@mcp.tool(description="Mueve o renombra un archivo o carpeta dentro del sandbox.")
|
||||||
|
def move_file(src: str, dest: str) -> str:
|
||||||
|
src_path = safe_path(src)
|
||||||
|
dest_path = safe_path(dest)
|
||||||
|
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
src_path.rename(dest_path)
|
||||||
|
return "Movimiento completado."
|
||||||
|
|
||||||
|
@mcp.tool(description="Elimina todos los archivos y subcarpetas dentro de una carpeta del sandbox.")
|
||||||
|
def clear_folder(path: str) -> str:
|
||||||
|
folder_path = safe_path(path)
|
||||||
|
if not folder_path.is_dir():
|
||||||
|
raise NotADirectoryError("La ruta no es una carpeta.")
|
||||||
|
for item in folder_path.iterdir():
|
||||||
|
if item.is_file() or item.is_symlink():
|
||||||
|
item.unlink()
|
||||||
|
elif item.is_dir():
|
||||||
|
shutil.rmtree(item)
|
||||||
|
return "Carpeta vaciada."
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
mcp.run(transport="streamable-http", host="127.0.0.1", port=4201, path="/fs")
|
||||||
|
|
||||||
@@ -87,6 +87,6 @@ def is_prime(n: int) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# mcp.run(transport="streamable-http", host="127.0.0.1", port=4200, path="/math")
|
mcp.run(transport="streamable-http", host="127.0.0.1", port=4200, path="/math")
|
||||||
|
|
||||||
mcp.run(transport="stdio")
|
# mcp.run(transport="stdio")
|
||||||
@@ -66,3 +66,4 @@ def current_timestamp() -> float:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mcp.run(transport="streamable-http", host="127.0.0.1", port=4300, path="/tools")
|
mcp.run(transport="streamable-http", host="127.0.0.1", port=4300, path="/tools")
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
from src.Llms.Modelos.Base_model import ModeloABC
|
||||||
|
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||||
|
from typing import AsyncGenerator, Union
|
||||||
|
from src.ConexionApis.Ollama_cliente import OllamaCliente # Asegúrate de importar correctamente
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
class ModeloOllama(ModeloABC):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
cliente: OllamaCliente,
|
||||||
|
model: str = "llama3",
|
||||||
|
id: str = None,
|
||||||
|
temperature: float = 0.7,
|
||||||
|
top_p: float = 1.0,
|
||||||
|
top_k: int = None,
|
||||||
|
frecuencia_penalizacion: float = 0.0,
|
||||||
|
num_tokens_maximos: int = 512
|
||||||
|
):
|
||||||
|
if not isinstance(cliente, OllamaCliente):
|
||||||
|
raise TypeError("El parámetro 'cliente' debe ser una instancia de OllamaCliente")
|
||||||
|
|
||||||
|
|
||||||
|
self.id = id if id else GeneradorIDUnico("MOOL").generar()
|
||||||
|
super().__init__(
|
||||||
|
model=model,
|
||||||
|
temperature=temperature,
|
||||||
|
top_p=top_p,
|
||||||
|
top_k=top_k,
|
||||||
|
frecuencia_penalizacion=frecuencia_penalizacion,
|
||||||
|
num_tokens_maximos=num_tokens_maximos
|
||||||
|
)
|
||||||
|
self.cliente = cliente
|
||||||
|
|
||||||
|
async def responder(
|
||||||
|
self,
|
||||||
|
prompt: str,
|
||||||
|
system_prompt: str = "",
|
||||||
|
stream: bool = False,
|
||||||
|
**kwargs
|
||||||
|
) -> Union[str, AsyncGenerator[str, None]]:
|
||||||
|
messages = []
|
||||||
|
if system_prompt:
|
||||||
|
messages.append({"role": "system", "content": system_prompt})
|
||||||
|
messages.append({"role": "user", "content": prompt})
|
||||||
|
|
||||||
|
def sync_call():
|
||||||
|
return self.cliente.chat_completion(
|
||||||
|
model=self.model,
|
||||||
|
messages=messages,
|
||||||
|
temperature=self.temperature,
|
||||||
|
top_p=self.top_p,
|
||||||
|
max_tokens=self.num_tokens_maximos,
|
||||||
|
frequency_penalty=self.frecuencia_penalizacion,
|
||||||
|
stream=stream,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
resultado = await loop.run_in_executor(None, sync_call)
|
||||||
|
|
||||||
|
if stream:
|
||||||
|
async def generador():
|
||||||
|
for token in resultado:
|
||||||
|
yield token
|
||||||
|
return generador()
|
||||||
|
else:
|
||||||
|
return resultado.choices[0].message.content
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
from loguru import logger
|
||||||
|
from sqlalchemy import Column, Integer, String, Text, TIMESTAMP
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
|
||||||
|
from src.ArquitectureLayer.Model import Model_base
|
||||||
|
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
||||||
|
from src.Credenciales.postgres_credencial import PostgresCredencial
|
||||||
|
|
||||||
|
class LoggerDB:
|
||||||
|
_sink_removido = False # ← evita múltiples remove() si se crean varias instancias
|
||||||
|
|
||||||
|
def __init__(self, credencial: PostgresCredencial, nombre_tabla: str, created_by: str = None):
|
||||||
|
|
||||||
|
# 🔥 Elimina todos los sinks activos, incluso los automáticos
|
||||||
|
logger.remove()
|
||||||
|
|
||||||
|
self.conexion = PostgresConexion(credencial)
|
||||||
|
self.engine = self.conexion.get_engine()
|
||||||
|
self.Session = sessionmaker(bind=self.engine)
|
||||||
|
self.nombre_tabla = nombre_tabla
|
||||||
|
self.created_by = created_by
|
||||||
|
|
||||||
|
self.modelo_logger = self._generar_modelo_logger()
|
||||||
|
self._crear_tabla_si_no_existe()
|
||||||
|
logger.add(self._sink, level="DEBUG")
|
||||||
|
|
||||||
|
def _generar_modelo_logger(self):
|
||||||
|
class LoggerTable(Model_base):
|
||||||
|
__tablename__ = self.nombre_tabla
|
||||||
|
__table_args__ = {'extend_existing': True} # 👈 Esta línea evita el error
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
nivel = Column(String, nullable=False)
|
||||||
|
mensaje = Column(Text, nullable=False)
|
||||||
|
fecha = Column(TIMESTAMP(timezone=True), nullable=False)
|
||||||
|
modulo = Column(String, nullable=True)
|
||||||
|
funcion = Column(String, nullable=True)
|
||||||
|
linea = Column(Integer, nullable=True)
|
||||||
|
return LoggerTable
|
||||||
|
|
||||||
|
def _crear_tabla_si_no_existe(self):
|
||||||
|
self.modelo_logger.__table__.create(self.engine, checkfirst=True)
|
||||||
|
|
||||||
|
def _sink(self, message):
|
||||||
|
record = message.record
|
||||||
|
try:
|
||||||
|
session = self.Session()
|
||||||
|
log_entry = self.modelo_logger(
|
||||||
|
nivel=record["level"].name,
|
||||||
|
mensaje=record["message"],
|
||||||
|
fecha=record["time"],
|
||||||
|
modulo=record["module"],
|
||||||
|
funcion=record["function"],
|
||||||
|
linea=record["line"],
|
||||||
|
sys_created_by=self.created_by
|
||||||
|
)
|
||||||
|
session.add(log_entry)
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
print(f"[LoggerDB] Error guardando log en BD: {e}")
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
import random
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .Tab import Tab
|
||||||
|
|
||||||
|
class ElementoWeb:
|
||||||
|
def __init__(self, tab: "Tab", object_id: Optional[str]):
|
||||||
|
self.tab = tab
|
||||||
|
self.object_id = object_id
|
||||||
|
self._node_id = None # Lazy resolved
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_node(cls, tab: "Tab", node_id: int) -> "ElementoWeb":
|
||||||
|
inst = cls(tab, object_id=None)
|
||||||
|
inst._node_id = node_id
|
||||||
|
return inst
|
||||||
|
|
||||||
|
async def _asegurar_object_id(self):
|
||||||
|
if not self.object_id and self._node_id:
|
||||||
|
try:
|
||||||
|
resolved = await self.tab._enviar("DOM.resolveNode", {"nodeId": self._node_id})
|
||||||
|
self.object_id = resolved["object"]["objectId"]
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ No se pudo resolver objectId desde nodeId: {e}")
|
||||||
|
|
||||||
|
async def scroll_into_view(self):
|
||||||
|
try:
|
||||||
|
await self._asegurar_object_id()
|
||||||
|
await self.tab._enviar("Runtime.callFunctionOn", {
|
||||||
|
"objectId": self.object_id,
|
||||||
|
"functionDeclaration": "function() { this.scrollIntoView({block: 'center'}); }",
|
||||||
|
"awaitPromise": True
|
||||||
|
})
|
||||||
|
if self.tab.verbose:
|
||||||
|
print("📜 Elemento desplazado a la vista.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al hacer scroll hacia el elemento: {e}")
|
||||||
|
|
||||||
|
async def click(self):
|
||||||
|
try:
|
||||||
|
await self.scroll_into_view()
|
||||||
|
await self._asegurar_object_id()
|
||||||
|
if not self.object_id:
|
||||||
|
raise ValueError("No se puede obtener objectId del elemento para hacer click.")
|
||||||
|
|
||||||
|
# Intenta obtener coordenadas del nodo
|
||||||
|
node_result = await self.tab._enviar("DOM.describeNode", {
|
||||||
|
"objectId": self.object_id
|
||||||
|
})
|
||||||
|
node_id = node_result["node"]["nodeId"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
box_model = await self.tab._enviar("DOM.getBoxModel", {"nodeId": node_id})
|
||||||
|
content = box_model["model"]["content"]
|
||||||
|
x = (content[0] + content[2]) / 2
|
||||||
|
y = (content[1] + content[5]) / 2
|
||||||
|
except:
|
||||||
|
quads_result = await self.tab._enviar("DOM.getContentQuads", {"nodeId": node_id})
|
||||||
|
quad = quads_result["quads"][0]
|
||||||
|
x = (quad[0] + quad[4]) / 2
|
||||||
|
y = (quad[1] + quad[5]) / 2
|
||||||
|
|
||||||
|
# 🧠 Enfocar el elemento antes de clickear
|
||||||
|
await self.tab._enviar("DOM.focus", {
|
||||||
|
"objectId": self.object_id
|
||||||
|
})
|
||||||
|
|
||||||
|
# 🎯 Movimiento humanoide opcional
|
||||||
|
start_x, start_y = x + random.uniform(-100, 100), y + random.uniform(-100, 100)
|
||||||
|
steps = random.randint(5, 12)
|
||||||
|
for i in range(1, steps + 1):
|
||||||
|
curr_x = start_x + (x - start_x) * i / steps + random.uniform(-1, 1)
|
||||||
|
curr_y = start_y + (y - start_y) * i / steps + random.uniform(-1, 1)
|
||||||
|
await self.tab._enviar("Input.dispatchMouseEvent", {
|
||||||
|
"type": "mouseMoved",
|
||||||
|
"x": curr_x,
|
||||||
|
"y": curr_y,
|
||||||
|
})
|
||||||
|
await asyncio.sleep(random.uniform(0.01, 0.05))
|
||||||
|
|
||||||
|
# 👆 Mouse Down
|
||||||
|
await self.tab._enviar("Input.dispatchMouseEvent", {
|
||||||
|
"type": "mousePressed",
|
||||||
|
"x": x,
|
||||||
|
"y": y,
|
||||||
|
"button": "left",
|
||||||
|
"clickCount": 1
|
||||||
|
})
|
||||||
|
|
||||||
|
await asyncio.sleep(random.uniform(0.05, 0.15))
|
||||||
|
|
||||||
|
# 👇 Mouse Up
|
||||||
|
await self.tab._enviar("Input.dispatchMouseEvent", {
|
||||||
|
"type": "mouseReleased",
|
||||||
|
"x": x,
|
||||||
|
"y": y,
|
||||||
|
"button": "left",
|
||||||
|
"clickCount": 1
|
||||||
|
})
|
||||||
|
|
||||||
|
await asyncio.sleep(random.uniform(0.01, 0.05))
|
||||||
|
|
||||||
|
# 🖱️ Click manual adicional
|
||||||
|
await self.tab._enviar("Input.dispatchMouseEvent", {
|
||||||
|
"type": "mouseClicked",
|
||||||
|
"x": x,
|
||||||
|
"y": y,
|
||||||
|
"button": "left",
|
||||||
|
"clickCount": 1
|
||||||
|
})
|
||||||
|
|
||||||
|
if self.tab.verbose:
|
||||||
|
print(f"🖱️ Click humano simulado en ({x:.1f}, {y:.1f})")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al hacer click físico: {e}")
|
||||||
|
print("🧪 Intentando fallback con JavaScript click()...")
|
||||||
|
await self.click_js()
|
||||||
|
|
||||||
|
async def click_js(self):
|
||||||
|
try:
|
||||||
|
await self._asegurar_object_id()
|
||||||
|
if not self.object_id:
|
||||||
|
print("⚠️ No se puede hacer click JS: objectId no disponible.")
|
||||||
|
return
|
||||||
|
await self.tab._enviar("Runtime.callFunctionOn", {
|
||||||
|
"objectId": self.object_id,
|
||||||
|
"functionDeclaration": "function() { this.click(); }",
|
||||||
|
"awaitPromise": True
|
||||||
|
})
|
||||||
|
if self.tab.verbose:
|
||||||
|
print("🖱️ Click simulado por JavaScript (element.click())")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al ejecutar click en JS: {e}")
|
||||||
|
|
||||||
|
async def obtener_texto(self) -> Optional[str]:
|
||||||
|
try:
|
||||||
|
await self._asegurar_object_id()
|
||||||
|
result = await self.tab._enviar("Runtime.callFunctionOn", {
|
||||||
|
"objectId": self.object_id,
|
||||||
|
"functionDeclaration": "function() { return this.textContent; }",
|
||||||
|
"returnByValue": True
|
||||||
|
})
|
||||||
|
return result.get("result", {}).get("value")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al obtener texto del elemento: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def escribir_texto(self, texto: str):
|
||||||
|
try:
|
||||||
|
await self._asegurar_object_id()
|
||||||
|
await self.tab._enviar("Runtime.callFunctionOn", {
|
||||||
|
"objectId": self.object_id,
|
||||||
|
"functionDeclaration": f"function() {{ this.value = {json.dumps(texto)}; this.dispatchEvent(new Event('input')); }}",
|
||||||
|
"awaitPromise": True
|
||||||
|
})
|
||||||
|
if self.tab.verbose:
|
||||||
|
print(f"⌨️ Texto escrito en elemento: '{texto}'")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al escribir texto: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
async def encontrar_hijo_clickeable(self) -> Optional["ElementoWeb"]:
|
||||||
|
try:
|
||||||
|
await self._asegurar_object_id()
|
||||||
|
resultado = await self.tab._enviar("Runtime.callFunctionOn", {
|
||||||
|
"objectId": self.object_id,
|
||||||
|
"functionDeclaration": """
|
||||||
|
function() {
|
||||||
|
const candidatos = this.querySelectorAll("span, div, a, button");
|
||||||
|
for (const el of candidatos) {
|
||||||
|
const style = window.getComputedStyle(el);
|
||||||
|
const visible = style.display !== "none" && style.visibility !== "hidden";
|
||||||
|
const interactivo = style.pointerEvents !== "none";
|
||||||
|
if (visible && interactivo) return el;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
"returnByValue": False
|
||||||
|
})
|
||||||
|
if "result" in resultado and "objectId" in resultado["result"]:
|
||||||
|
return ElementoWeb(self.tab, resultado["result"]["objectId"])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ No se pudo encontrar hijo clickeable: {e}")
|
||||||
|
return None
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
import subprocess
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
|
|
||||||
|
class Navegador:
|
||||||
|
def __init__(self,
|
||||||
|
chrome_path: str,
|
||||||
|
user_data_dir: str,
|
||||||
|
id: Optional[int] = None,
|
||||||
|
download_dir: Optional[str] = None,
|
||||||
|
debugging_port: int = 9222,
|
||||||
|
headless: bool = False,
|
||||||
|
user_agent: Optional[str] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"):
|
||||||
|
self.chrome_path = chrome_path
|
||||||
|
self.user_data_dir = user_data_dir
|
||||||
|
self.id = id
|
||||||
|
self.download_dir = download_dir or os.path.join(self.user_data_dir, "downloads")
|
||||||
|
self.debugging_port = debugging_port
|
||||||
|
self.headless = headless
|
||||||
|
self.user_agent = user_agent
|
||||||
|
self.chrome_process: Optional[subprocess.Popen] = None
|
||||||
|
|
||||||
|
async def _esperar_debugger(self, timeout=10):
|
||||||
|
url = f"http://127.0.0.1:{self.debugging_port}/json"
|
||||||
|
for _ in range(timeout * 10): # 10 intentos por segundo
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
print("✅ Chrome listo para debugging.")
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
raise RuntimeError("❌ Chrome no respondió en el puerto de debugging.")
|
||||||
|
|
||||||
|
def _preconfigurar_preferencias(self):
|
||||||
|
prefs_path = os.path.join(self.user_data_dir, "Default", "Preferences")
|
||||||
|
os.makedirs(os.path.dirname(prefs_path), exist_ok=True)
|
||||||
|
os.makedirs(self.download_dir, exist_ok=True)
|
||||||
|
|
||||||
|
prefs = {
|
||||||
|
"profile": {
|
||||||
|
"exit_type": "Normal",
|
||||||
|
"exited_cleanly": True
|
||||||
|
},
|
||||||
|
"browser": {
|
||||||
|
"has_seen_welcome_page": True
|
||||||
|
},
|
||||||
|
"distribution": {
|
||||||
|
"skip_first_run_ui": True
|
||||||
|
},
|
||||||
|
"download": {
|
||||||
|
"default_directory": self.download_dir,
|
||||||
|
"prompt_for_download": False,
|
||||||
|
"directory_upgrade": True,
|
||||||
|
"extensions_to_open": ""
|
||||||
|
},
|
||||||
|
"savefile": {
|
||||||
|
"default_directory": self.download_dir
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.path.exists(prefs_path):
|
||||||
|
try:
|
||||||
|
with open(prefs_path, "r", encoding="utf-8") as f:
|
||||||
|
existing = json.load(f)
|
||||||
|
existing.update(prefs)
|
||||||
|
prefs = existing
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
with open(prefs_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(prefs, f, indent=2)
|
||||||
|
|
||||||
|
def _build_args(self):
|
||||||
|
os.makedirs(self.user_data_dir, exist_ok=True)
|
||||||
|
self._preconfigurar_preferencias()
|
||||||
|
|
||||||
|
args = [
|
||||||
|
f"--remote-debugging-port={self.debugging_port}",
|
||||||
|
f"--user-data-dir={self.user_data_dir}",
|
||||||
|
"--disable-blink-features=AutomationControlled",
|
||||||
|
"--no-sandbox",
|
||||||
|
# "--disable-web-security",
|
||||||
|
# "--disable-extensions",
|
||||||
|
# "--disable-dev-shm-usage",
|
||||||
|
"--disable-infobars",
|
||||||
|
"--disable-popup-blocking",
|
||||||
|
"--disable-default-apps",
|
||||||
|
"--mute-audio",
|
||||||
|
"--window-size=1024,1024",
|
||||||
|
"--no-first-run",
|
||||||
|
"--no-default-browser-check",
|
||||||
|
"--disable-features=DefaultBrowserPrompt",
|
||||||
|
"--disable-component-update",
|
||||||
|
"--disable-background-networking",
|
||||||
|
"--disable-sync",
|
||||||
|
"--disable-translate",
|
||||||
|
"--disable-background-timer-throttling",
|
||||||
|
"--disable-client-side-phishing-detection",
|
||||||
|
"--disable-component-extensions-with-background-pages",
|
||||||
|
"--metrics-recording-only",
|
||||||
|
"--safebrowsing-disable-auto-update",
|
||||||
|
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
if self.headless:
|
||||||
|
args.append("--headless=new")
|
||||||
|
|
||||||
|
if self.user_agent:
|
||||||
|
args.append(f"--user-agent={self.user_agent}")
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def inyectar_spoof_chrome(self):
|
||||||
|
script = """
|
||||||
|
window.chrome = {
|
||||||
|
app: {
|
||||||
|
isInstalled: false,
|
||||||
|
InstallState: {
|
||||||
|
DISABLED: 'disabled',
|
||||||
|
INSTALLED: 'installed',
|
||||||
|
NOT_INSTALLED: 'not_installed'
|
||||||
|
},
|
||||||
|
RunningState: {
|
||||||
|
CANNOT_RUN: 'cannot_run',
|
||||||
|
READY_TO_RUN: 'ready_to_run',
|
||||||
|
RUNNING: 'running'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
runtime: {
|
||||||
|
PlatformOs: { MAC: 'mac', WIN: 'win', ANDROID: 'android', CROS: 'cros', LINUX: 'linux', OPENBSD: 'openbsd' },
|
||||||
|
PlatformArch: { ARM: 'arm', X86_32: 'x86-32', X86_64: 'x86-64' },
|
||||||
|
PlatformNaclArch: { ARM: 'arm', X86_32: 'x86-32', X86_64: 'x86-64' },
|
||||||
|
RequestUpdateCheckStatus: { THROTTLED: 'throttled', NO_UPDATE: 'no_update', UPDATE_AVAILABLE: 'update_available' },
|
||||||
|
OnInstalledReason: { INSTALL: 'install', UPDATE: 'update', CHROME_UPDATE: 'chrome_update', SHARED_MODULE_UPDATE: 'shared_module_update' },
|
||||||
|
OnRestartRequiredReason: { APP_UPDATE: 'app_update', OS_UPDATE: 'os_update', PERIODIC: 'periodic' }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
"""
|
||||||
|
|
||||||
|
url = f"http://127.0.0.1:{self.debugging_port}/json"
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url) as resp:
|
||||||
|
targets = await resp.json()
|
||||||
|
|
||||||
|
for target in targets:
|
||||||
|
if "webSocketDebuggerUrl" not in target:
|
||||||
|
continue
|
||||||
|
|
||||||
|
target_id = target["id"]
|
||||||
|
async with session.post(
|
||||||
|
f"http://127.0.0.1:{self.debugging_port}/json/protocol",
|
||||||
|
json={"targetId": target_id}
|
||||||
|
):
|
||||||
|
pass # CDP protocol fetch optional
|
||||||
|
|
||||||
|
async with session.post(
|
||||||
|
f"http://127.0.0.1:{self.debugging_port}/json/send",
|
||||||
|
json={
|
||||||
|
"id": 1,
|
||||||
|
"method": "Page.addScriptToEvaluateOnNewDocument",
|
||||||
|
"params": {"source": script}
|
||||||
|
}
|
||||||
|
) as inject_resp:
|
||||||
|
if inject_resp.status == 200:
|
||||||
|
print("✅ chrome.* spoof inyectado.")
|
||||||
|
|
||||||
|
|
||||||
|
async def iniciar(self):
|
||||||
|
args = self._build_args()
|
||||||
|
self.chrome_process = subprocess.Popen([self.chrome_path] + args)
|
||||||
|
print(f"Chrome iniciado (headless={self.headless}). Esperando disponibilidad del debugger...")
|
||||||
|
await self._esperar_debugger()
|
||||||
|
await self.inyectar_spoof_chrome()
|
||||||
|
|
||||||
|
async def cerrar(self):
|
||||||
|
if self.chrome_process and self.chrome_process.poll() is None:
|
||||||
|
self.chrome_process.terminate()
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(asyncio.to_thread(self.chrome_process.wait), timeout=5)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
self.chrome_process.kill()
|
||||||
|
print("🛑 Chrome cerrado correctamente.")
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
import aiohttp
|
||||||
|
import websockets
|
||||||
|
import json
|
||||||
|
import asyncio
|
||||||
|
from .Tab import Tab
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Scrapper:
|
||||||
|
def __init__(self, debugging_url: str = "http://127.0.0.1:9222"):
|
||||||
|
self.debugging_url = debugging_url
|
||||||
|
self.tabs: list[Tab] = []
|
||||||
|
|
||||||
|
async def _crear_tab_websocket_url(self, target_url: str = "about:blank") -> str:
|
||||||
|
"""
|
||||||
|
Crea una nueva pestaña usando el método oficial Target.createTarget
|
||||||
|
y devuelve su WebSocketDebuggerUrl.
|
||||||
|
"""
|
||||||
|
# 1. Obtener el WebSocket general del browser (root)
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(f"{self.debugging_url}/json/version") as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
raise RuntimeError("No se pudo obtener información del navegador")
|
||||||
|
data = await resp.json()
|
||||||
|
browser_ws_url = data["webSocketDebuggerUrl"]
|
||||||
|
|
||||||
|
# 2. Conectarse al WebSocket del browser
|
||||||
|
async with websockets.connect(browser_ws_url) as websocket:
|
||||||
|
# 3. Enviar comando para crear target
|
||||||
|
msg_id = 1
|
||||||
|
await websocket.send(json.dumps({
|
||||||
|
"id": msg_id,
|
||||||
|
"method": "Target.createTarget",
|
||||||
|
"params": {
|
||||||
|
"url": target_url,
|
||||||
|
"newWindow": False
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
# 4. Esperar respuesta con el targetId
|
||||||
|
while True:
|
||||||
|
respuesta = await websocket.recv()
|
||||||
|
data = json.loads(respuesta)
|
||||||
|
if data.get("id") == msg_id:
|
||||||
|
target_id = data["result"]["targetId"]
|
||||||
|
break
|
||||||
|
|
||||||
|
# 5. Esperar a que el target aparezca en /json
|
||||||
|
for _ in range(30): # máximo ~3 segundos
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(f"{self.debugging_url}/json") as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
tabs = await resp.json()
|
||||||
|
for tab in tabs:
|
||||||
|
if tab.get("id") == target_id:
|
||||||
|
return tab["webSocketDebuggerUrl"]
|
||||||
|
|
||||||
|
raise RuntimeError("No se pudo obtener el WebSocket de la nueva pestaña")
|
||||||
|
|
||||||
|
async def nueva_tab(self, url: str = "", wait_time: float = 5.0) -> Tab:
|
||||||
|
websocket_url = await self._crear_tab_websocket_url()
|
||||||
|
tab = await Tab.crear_desde_websocket(websocket_url)
|
||||||
|
self.tabs.append(tab)
|
||||||
|
|
||||||
|
if url:
|
||||||
|
print(f"🌍 Navegando a: {url}")
|
||||||
|
await tab.navegar(url, wait_time)
|
||||||
|
else:
|
||||||
|
print("⚠️ No se especificó URL. La pestaña se creó pero no se navegó a ninguna página.")
|
||||||
|
|
||||||
|
return tab
|
||||||
|
|
||||||
|
async def cerrar_todos(self):
|
||||||
|
for tab in list(self.tabs):
|
||||||
|
await tab.cerrar()
|
||||||
|
self.tabs.clear()
|
||||||
|
|
||||||
|
def get_tab(self, identifier: str) -> Optional[Tab]:
|
||||||
|
"""
|
||||||
|
Devuelve una instancia de Tab según su WebSocket URL o su ID final (extraído del WebSocket URL).
|
||||||
|
Acepta:
|
||||||
|
- ws_url completo: ws://127.0.0.1:9222/devtools/page/XYZ
|
||||||
|
- id directo: XYZ
|
||||||
|
"""
|
||||||
|
for tab in self.tabs:
|
||||||
|
# Comparar directamente contra ws_url
|
||||||
|
if tab.ws_url == identifier:
|
||||||
|
return tab
|
||||||
|
|
||||||
|
# Comparar contra el ID extraído
|
||||||
|
ws_id = tab.ws_url.rsplit("/", 1)[-1]
|
||||||
|
if ws_id == identifier:
|
||||||
|
return tab
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def obtener_tabs_existentes(self) -> list[Tab]:
|
||||||
|
"""
|
||||||
|
Recupera todas las pestañas de tipo 'page' que no están ya en self.tabs,
|
||||||
|
las conecta y devuelve como lista. Muestra resumen limpio por consola.
|
||||||
|
"""
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(f"{self.debugging_url}/json") as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
raise RuntimeError("No se pudo obtener la lista de pestañas")
|
||||||
|
|
||||||
|
tabs_info = await resp.json()
|
||||||
|
|
||||||
|
print("\n🧾 Pestañas activas (filtradas: solo 'type': 'page'):\n")
|
||||||
|
nuevas_tabs = []
|
||||||
|
for idx, tab_info in enumerate(tabs_info, start=1):
|
||||||
|
tipo = tab_info.get("type")
|
||||||
|
if tipo != "page":
|
||||||
|
continue # Filtrar todo lo que no sea página visible
|
||||||
|
|
||||||
|
ws_url = tab_info.get("webSocketDebuggerUrl")
|
||||||
|
tab_id = tab_info.get("id")
|
||||||
|
title = tab_info.get("title", "<Sin título>")
|
||||||
|
url = tab_info.get("url", "<Sin URL>")
|
||||||
|
|
||||||
|
# Verifica si ya la tienes cargada
|
||||||
|
if any(t.ws_url == ws_url for t in self.tabs):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Conectar
|
||||||
|
try:
|
||||||
|
tab = await Tab.crear_desde_websocket(ws_url)
|
||||||
|
self.tabs.append(tab)
|
||||||
|
nuevas_tabs.append(tab)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ No se pudo conectar a pestaña {tab_id}: {e}")
|
||||||
|
|
||||||
|
if not nuevas_tabs:
|
||||||
|
print("⚠️ No se encontraron nuevas pestañas para agregar.\n")
|
||||||
|
|
||||||
|
return nuevas_tabs
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import base64
|
||||||
|
import websockets
|
||||||
|
from typing import Optional, List
|
||||||
|
from .ElementoWeb import ElementoWeb
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Tab:
|
||||||
|
def __init__(self, websocket: websockets.WebSocketClientProtocol, ws_url: str, verbose: bool = True):
|
||||||
|
self.websocket = websocket
|
||||||
|
self.ws_url = ws_url
|
||||||
|
self._message_id = 0
|
||||||
|
self._pending = {}
|
||||||
|
self._load_event = asyncio.Event()
|
||||||
|
self.verbose = verbose
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
await self.cerrar()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def crear_desde_websocket(cls, ws_url: str) -> "Tab":
|
||||||
|
websocket = await websockets.connect(ws_url, max_size=10 * 1024 * 1024)
|
||||||
|
tab = cls(websocket, ws_url)
|
||||||
|
asyncio.create_task(tab._recibir_eventos())
|
||||||
|
await tab._enviar("Page.enable")
|
||||||
|
await tab._enviar("Network.enable")
|
||||||
|
return tab
|
||||||
|
|
||||||
|
async def _recibir_eventos(self):
|
||||||
|
async for mensaje in self.websocket:
|
||||||
|
data = json.loads(mensaje)
|
||||||
|
if "id" in data and data["id"] in self._pending:
|
||||||
|
future = self._pending.pop(data["id"])
|
||||||
|
if "result" in data:
|
||||||
|
future.set_result(data["result"])
|
||||||
|
elif "error" in data:
|
||||||
|
future.set_exception(Exception(data["error"]))
|
||||||
|
elif data.get("method") == "Page.loadEventFired":
|
||||||
|
self._load_event.set()
|
||||||
|
|
||||||
|
async def _enviar(self, metodo: str, parametros: Optional[dict] = None, timeout: float = 10.0) -> dict:
|
||||||
|
self._message_id += 1
|
||||||
|
msg_id = self._message_id
|
||||||
|
mensaje = {
|
||||||
|
"id": msg_id,
|
||||||
|
"method": metodo,
|
||||||
|
"params": parametros or {}
|
||||||
|
}
|
||||||
|
|
||||||
|
future = asyncio.get_event_loop().create_future()
|
||||||
|
self._pending[msg_id] = future
|
||||||
|
await self.websocket.send(json.dumps(mensaje))
|
||||||
|
return await asyncio.wait_for(future, timeout=timeout)
|
||||||
|
|
||||||
|
async def navegar(self, url: str, wait_time: float = 5.0):
|
||||||
|
self._load_event.clear()
|
||||||
|
if self.verbose:
|
||||||
|
print(f"🌍 Navegando a: {url}")
|
||||||
|
await self._enviar("Page.navigate", {"url": url})
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(self._load_event.wait(), timeout=wait_time)
|
||||||
|
if self.verbose:
|
||||||
|
print("✅ Página cargada correctamente.")
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
print(f"⚠️ Tiempo de espera agotado ({wait_time}s) al cargar la página.")
|
||||||
|
|
||||||
|
async def evaluar_js(self, js_code: str) -> Optional[str]:
|
||||||
|
try:
|
||||||
|
result = await self._enviar("Runtime.evaluate", {
|
||||||
|
"expression": js_code,
|
||||||
|
"returnByValue": True
|
||||||
|
})
|
||||||
|
if "exceptionDetails" in result:
|
||||||
|
raise Exception(result["exceptionDetails"])
|
||||||
|
return result.get("result", {}).get("value")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al ejecutar JS: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def inyectar_archivo_js(self, ruta_archivo: str, reemplazos: dict = None) -> Optional[str]:
|
||||||
|
if not os.path.exists(ruta_archivo):
|
||||||
|
print(f"❌ Archivo JS no encontrado: {ruta_archivo}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
with open(ruta_archivo, "r", encoding="utf-8") as f:
|
||||||
|
js_code = f.read()
|
||||||
|
|
||||||
|
if reemplazos:
|
||||||
|
for key, value in reemplazos.items():
|
||||||
|
js_code = js_code.replace(f"{{{{{key}}}}}", str(value))
|
||||||
|
|
||||||
|
# 🔧 Eliminamos el `return` externo
|
||||||
|
js_code_final = f"(async () => {{\n{js_code}\n}})();"
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = await self._enviar("Runtime.evaluate", {
|
||||||
|
"expression": js_code_final,
|
||||||
|
"returnByValue": True
|
||||||
|
})
|
||||||
|
if "exceptionDetails" in result:
|
||||||
|
raise Exception(result["exceptionDetails"])
|
||||||
|
return result.get("result", {}).get("value")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al inyectar JS desde {ruta_archivo}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def obtener_user_agent(self) -> Optional[str]:
|
||||||
|
return await self.evaluar_js("navigator.userAgent")
|
||||||
|
|
||||||
|
async def capturar_screenshot(self, output_path: str = "screenshot.png"):
|
||||||
|
try:
|
||||||
|
result = await self._enviar("Page.captureScreenshot")
|
||||||
|
data = result["data"]
|
||||||
|
with open(output_path, "wb") as f:
|
||||||
|
f.write(base64.b64decode(data))
|
||||||
|
if self.verbose:
|
||||||
|
print(f"📸 Screenshot guardado como {output_path}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al capturar screenshot: {e}")
|
||||||
|
|
||||||
|
async def cerrar(self):
|
||||||
|
try:
|
||||||
|
if not self.websocket.closed:
|
||||||
|
await self.websocket.close()
|
||||||
|
if self.verbose:
|
||||||
|
print("🛑 WebSocket cerrado.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al cerrar pestaña: {e}")
|
||||||
|
|
||||||
|
async def obtener_html_completo(self) -> Optional[str]:
|
||||||
|
try:
|
||||||
|
result = await self._enviar("Runtime.evaluate", {
|
||||||
|
"expression": "document.documentElement.outerHTML",
|
||||||
|
"returnByValue": True
|
||||||
|
})
|
||||||
|
return result.get("result", {}).get("value")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al obtener HTML: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def obtener_dominio(self) -> Optional[str]:
|
||||||
|
try:
|
||||||
|
dominio = await self.evaluar_js("window.location.hostname")
|
||||||
|
if self.verbose and dominio:
|
||||||
|
print(f"🌐 Dominio actual: {dominio}")
|
||||||
|
return dominio
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al obtener dominio: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def get_element_by_selector_node(self, selector: str) -> Optional["ElementoWeb"]:
|
||||||
|
try:
|
||||||
|
doc = await self._enviar("DOM.getDocument")
|
||||||
|
root_node_id = doc["root"]["nodeId"]
|
||||||
|
|
||||||
|
result = await self._enviar("DOM.querySelector", {
|
||||||
|
"nodeId": root_node_id,
|
||||||
|
"selector": selector
|
||||||
|
})
|
||||||
|
node_id = result.get("nodeId")
|
||||||
|
|
||||||
|
if not node_id:
|
||||||
|
print(f"⚠️ Nodo no encontrado con selector: {selector}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return ElementoWeb.from_node(self, node_id=node_id)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al buscar nodo desde DOM.querySelector: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def get_elements_by_css_selector(self, selector: str) -> List["ElementoWeb"]:
|
||||||
|
try:
|
||||||
|
result = await self._enviar("Runtime.evaluate", {
|
||||||
|
"expression": f'Array.from(document.querySelectorAll("{selector}"))',
|
||||||
|
"objectGroup": "grupo_elementos_css",
|
||||||
|
"includeCommandLineAPI": True,
|
||||||
|
"returnByValue": False
|
||||||
|
})
|
||||||
|
array_id = result["result"]["objectId"]
|
||||||
|
props = await self._enviar("Runtime.getProperties", {
|
||||||
|
"objectId": array_id,
|
||||||
|
"ownProperties": True
|
||||||
|
})
|
||||||
|
elementos = []
|
||||||
|
for prop in props["result"]:
|
||||||
|
if "value" in prop and "objectId" in prop["value"]:
|
||||||
|
elementos.append(ElementoWeb(self, prop["value"]["objectId"]))
|
||||||
|
if self.verbose:
|
||||||
|
print(f"🔍 Se encontraron {len(elementos)} elementos con el selector CSS '{selector}'.")
|
||||||
|
return elementos
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al buscar elementos por selector CSS '{selector}': {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
async def enfocar(self):
|
||||||
|
try:
|
||||||
|
await self._enviar("Page.bringToFront")
|
||||||
|
if self.verbose:
|
||||||
|
print("🪟 Pestaña enfocada (bringToFront).")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error al enfocar pestaña: {e}")
|
||||||
@@ -13,6 +13,30 @@ from src.ArquitectureLayer.Mapper import Mapper_base
|
|||||||
from src.ArquitectureLayer.Model import Model_base
|
from src.ArquitectureLayer.Model import Model_base
|
||||||
from src.ArquitectureLayer.Repo import Repo_base
|
from src.ArquitectureLayer.Repo import Repo_base
|
||||||
|
|
||||||
|
from src.Credenciales.postgres_credencial import PostgresCredencial # Asegúrate de tener esta clase implementada correctamente
|
||||||
|
|
||||||
|
|
||||||
|
titulo = os.getenv('DB_TITLE')
|
||||||
|
usuario = os.getenv('DB_USER')
|
||||||
|
passwrd = os.getenv('DB_PASSWORD')
|
||||||
|
host = os.getenv('DB_HOST')
|
||||||
|
port = os.getenv('DB_PORT')
|
||||||
|
db_name = os.getenv('DB_NAME')
|
||||||
|
|
||||||
|
|
||||||
|
db_credencial = PostgresCredencial(
|
||||||
|
titulo=titulo,
|
||||||
|
user=usuario,
|
||||||
|
password=passwrd,
|
||||||
|
host=host,
|
||||||
|
port=port,
|
||||||
|
dbname=db_name
|
||||||
|
)
|
||||||
|
|
||||||
|
# from entrypoint.init_db import db_credencial
|
||||||
|
from src.Logger.logger_db import LoggerDB, logger
|
||||||
|
LoggerDB(db_credencial, "logger_textos", created_by="sistema")
|
||||||
|
|
||||||
|
|
||||||
from src.base import Base # Este es tu declarative_base()
|
from src.base import Base # Este es tu declarative_base()
|
||||||
|
|
||||||
@@ -37,11 +61,11 @@ def generar_tabla_nota_para_biblioteca(biblioteca_nombre: str, vector_dim: int,
|
|||||||
Genera una tabla dinámica y modelo ORM para una biblioteca dada, con campos vectoriales y campos del sistema.
|
Genera una tabla dinámica y modelo ORM para una biblioteca dada, con campos vectoriales y campos del sistema.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
print(f"[INFO] Generando tabla para biblioteca: '{biblioteca_nombre}' con dimensión de vector: {vector_dim}")
|
logger.info(f"Generando tabla para biblioteca: '{biblioteca_nombre}' con dimensión de vector: {vector_dim}")
|
||||||
|
|
||||||
# Nombre SQL-safe
|
# Nombre SQL-safe
|
||||||
nombre_tabla = re.sub(r"[^a-zA-Z0-9_]", "_", biblioteca_nombre.strip().lower())
|
nombre_tabla = re.sub(r"[^a-zA-Z0-9_]", "_", biblioteca_nombre.strip().lower())
|
||||||
print(f"[DEBUG] Nombre de tabla SQL-safe: '{nombre_tabla}'")
|
logger.debug(f"Nombre de tabla SQL-safe: '{nombre_tabla}'")
|
||||||
|
|
||||||
# Modelo ORM dinámico
|
# Modelo ORM dinámico
|
||||||
class NotaModel(Base, Model_base):
|
class NotaModel(Base, Model_base):
|
||||||
@@ -57,15 +81,15 @@ def generar_tabla_nota_para_biblioteca(biblioteca_nombre: str, vector_dim: int,
|
|||||||
vector = Column(Vector(vector_dim), nullable=True)
|
vector = Column(Vector(vector_dim), nullable=True)
|
||||||
vector_resumen = Column(Vector(vector_dim), nullable=True)
|
vector_resumen = Column(Vector(vector_dim), nullable=True)
|
||||||
|
|
||||||
print(f"[INFO] Modelo ORM 'NotaModel' creado para la tabla '{nombre_tabla}'")
|
logger.info(f"Modelo ORM 'NotaModel' creado para la tabla '{nombre_tabla}'")
|
||||||
print(f"[DEBUG] Columnas del modelo: {[c.name for c in NotaModel.__table__.columns]}")
|
logger.debug(f"Columnas del modelo: {[c.name for c in NotaModel.__table__.columns]}")
|
||||||
print(f"[DEBUG] Tipos de columnas: {[str(c.type) for c in NotaModel.__table__.columns]}")
|
logger.debug(f"Tipos de columnas: {[str(c.type) for c in NotaModel.__table__.columns]}")
|
||||||
print(f"[DEBUG] Claves primarias: {[c.name for c in NotaModel.__table__.primary_key]}")
|
logger.debug(f"Claves primarias: {[c.name for c in NotaModel.__table__.primary_key]}")
|
||||||
|
|
||||||
return NotaModel.__table__, NotaModel
|
return NotaModel.__table__, NotaModel
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[ERROR] Error al generar la tabla y modelo ORM para '{biblioteca_nombre}': {e}")
|
logger.error(f"Error al generar la tabla y modelo ORM para '{biblioteca_nombre}': {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# ----------------------
|
# ----------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user