First commit

This commit is contained in:
remaster 2026-05-19 22:13:49 -06:00
commit 7e41eed17e
15 changed files with 4587 additions and 0 deletions

14
.env.example Normal file
View file

@ -0,0 +1,14 @@
# Copia este archivo a .env y rellena tus API keys
GROQ_API_KEY=gsk_...
ELEVENLABS_API_KEY=sk_...
# Opcional — búsqueda Google vía Serper (mejor que solo DuckDuckGo)
# SERPER_API_KEY=
# Zona horaria para «hoy» (IANA, ej. America/Mexico_City, Europe/Madrid, UTC)
# APP_TIMEZONE=America/Mexico_City
# GROQ_MODEL=llama-3.1-8b-instant
# GROQ_STT_MODEL=whisper-large-v3-turbo
# ELEVEN_VOICE_ID=B8gJV1IhpuegLxdpXFOE
# ELEVEN_MODEL=eleven_v3

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
.env
__pycache__/
*.py[cod]
.DS_Store
.venv/
venv/
*.egg-info/

161
CLAUDE.md Normal file
View file

@ -0,0 +1,161 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Running the app
```bash
pip install -r requirements.txt
cp .env.example .env # configure GROQ_API_KEY and ELEVENLABS_API_KEY
python main.py
# → http://localhost:8000
```
`live2dcubismcore.min.js` (~202 KB) is downloaded automatically on first run if missing.
## Architecture
**Backend** (`main.py`) connected to a vanilla JS frontend via one persistent WebSocket (`/ws`). Each connection holds a `SessionState` with conversation history, busy/cancel flags, TTS preference, and optional geo coordinates.
Supporting modules:
| File | Role |
|------|------|
| `websearch.py` | `web_search` tool — Serper → DuckDuckGo → Wikipedia |
| `weather.py` | Open-Meteo forecast + `parse_weather_request()` intent detection |
| `workspace.py` | Terminal tools when workspace mode is enabled |
### Message flow
```
Browser mic → PCM Int16 binary frame → /ws
→ Groq Whisper STT → { type: "transcript" }
→ (optional) weather prefetch → { type: "weather_widget" }
→ Groq LLM (history + tools) → { type: "chunk" } × N → { type: "done" }
→ ElevenLabs TTS (if tts_enabled) → { type: "tts_audio", data: base64mp3 }
```
Text input skips STT: browser sends `{ type: "question", text }` directly.
Wake word flow (Safari-compatible — no Web Speech API):
```
Continuous mic + VAD → { type: "wake_check", pcm: base64 }
→ Whisper on server → { type: "wake_ok", text? } | { type: "no_wake" }
→ if text: auto-send question; else: start follow-up recording with post-buffer
```
### WebSocket message types
| Direction | Type | Meaning |
|-----------|------|---------|
| client→server | `audio_config` | Set `lang`, `sample_rate`, `tts_enabled` |
| client→server | `geo_config` | Browser lat/lon for local weather |
| client→server | `wake_check` | Base64 PCM to detect wake word «Arisu» |
| client→server | `workspace_config` | Enable terminal + base directory path |
| client→server | `question` | Text prompt |
| client→server | `cancel` | Cancel in-flight LLM/TTS |
| client→server | `clear_history` | Reset session conversation history |
| client→server | binary | Raw PCM-16LE at 16 kHz (manual recording) |
| server→client | `transcript` | Whisper result |
| server→client | `wake_ok` | Wake word detected; optional `text` = remainder question |
| server→client | `no_wake` | Wake word not found |
| server→client | `thinking` | LLM started |
| server→client | `chunk` | LLM streaming delta (may contain `[tag]`) |
| server→client | `done` | LLM finished |
| server→client | `cancelled` | Response was cancelled |
| server→client | `busy` | Server already processing a request |
| server→client | `tts_audio` | Base64 MP3 |
| server→client | `tts_error` | ElevenLabs failed |
| server→client | `no_speech` | Audio below threshold or too short |
| server→client | `tool_event` | Tool invocation started |
| server→client | `tool_result` | Tool result preview |
| server→client | `weather_widget` | Structured weather data for chat card |
| server→client | `workspace_ok` | Workspace validated |
| server→client | `workspace_error` | Invalid workspace path |
| server→client | `history_cleared` | History reset confirmed |
| server→client | `error` | Generic error |
### Weather
[`weather.py`](weather.py) uses Open-Meteo (no API key). **Do not expose weather via LLM tool calling** — `llama-3.1-8b-instant` often generates malformed tool calls (`tool_use_failed`).
Instead, `_prefetch_weather()` in `main.py`:
1. Runs `parse_weather_request(user_text)` — regex intent + city extraction (aliases like Tokio→Tokyo).
2. Calls `get_weather(location, lang, session.geo_lat, session.geo_lon)`.
3. Sends `{ type: "weather_widget", data: {...} }` to the browser.
4. Injects weather text into the LLM messages as a system note (`DATOS DE CLIMA`).
Frontend renders the widget in `appendWeatherWidget()` with Bootstrap Icons mapped by WMO code icon key.
Geo: browser sends `{ type: "geo_config", lat, lon }` on WebSocket open if geolocation is allowed.
### Web search
[`websearch.py`](websearch.py) provides `web_search(query, lang)` via LLM tool calling. Fallback chain: Serper (if `SERPER_API_KEY`) → DuckDuckGo → Wikipedia.
`current_date_context(lang)` and `enrich_search_query()` inject today's date from `APP_TIMEZONE` (default `America/Mexico_City`) so «hoy» queries use the real year.
### Terminal / workspace mode
When `terminal_enabled` and a valid `workspace_path` are set via `workspace_config`, terminal tools are added alongside the always-available `web_search` tool.
### LLM tool calling
- Tools per session: `[WEB_SEARCH_TOOL]` + terminal tools if enabled.
- `_llm_completion()` retries **without tools** on Groq `tool_use_failed` (400) instead of crashing.
### Session memory
- `SessionState.history` stores up to `MAX_HISTORY` (20) user/assistant messages per WebSocket.
- History is trimmed FIFO when exceeded.
- Cleared via `clear_history` message or frontend "Clear chat" button.
### Wake word (browser)
Key constants in `app.js`:
| Constant | Value | Purpose |
|----------|-------|---------|
| `WAKE_SILENCE_MS` | 1700 | End capture after pause (allows gap between «Arisu» and question) |
| `WAKE_MAX_MS` | 9000 | Max utterance length for wake capture |
| `WAKE_PREROLL_MS` | 350 | Pre-buffer before speech detection |
| `WAKE_POST_BUFFER_MS` | 6000 | Buffer audio during server wake_check STT |
While `wakeChecking`, audio is stored in `wakePostBuffer` and prepended to follow-up recording on `wake_ok` without `text`.
### Audio pipeline (browser)
`getUserMedia` → `AudioContext` → `ScriptProcessor(4096)` → Float32 chunks → downsample to 16 kHz → `Int16Array` → binary WebSocket frame. The server always wraps received bytes as 16 kHz WAV regardless of the `audio_config` sample_rate field.
### Audio tags
The LLM is prompted to include ElevenLabs v3 emotion tags (`[laughs]`, `[giggles]`, `[excited]`, `[whispers]`, `[sighs]`, `[gasps]`) inline. `app.js:detectExpressions()` parses tags from streaming chunks before display and calls `avatar.setExpression(tag)`. Tags are stripped from chat text via `stripAudioTags()`. The full tagged text is sent to ElevenLabs TTS unchanged.
### Live2D (`live2d.js`)
`Live2DAvatar` wraps pixi-live2d-display. Key methods: `setLook(x,y)`, `startSpeaking(analyser?)`, `stopSpeaking()`, `setExpression(tag)`. Lip-sync uses `AnalyserNode` RMS from TTS playback when available; falls back to sinusoidal mouth animation. Model is anchored at `(0.5, 0)` with Y offset to show face+torso.
### TTS playback
Uses `AudioContext.decodeAudioData()` + `BufferSourceNode` + `AnalyserNode` (not `new Audio()`) to avoid browser autoplay blocking and drive lip-sync. Reuses `state.audioCtx` from the mic pipeline.
### i18n
Frontend UI strings are bilingual ES/EN via `I18N` object in `app.js`. UI language is independent of STT language (`langSelect`). Preference stored in `localStorage` key `arisu_ui_lang`.
## Key constraints
- API keys must be in `.env` — never hardcode in `main.py`.
- The server **always** uses `16000` Hz for WAV wrapping — the client always downsamples to 16 kHz before sending.
- RMS check (`< 0.015`) in `stopRecording()` discards silent/noise-only recordings before sending to Whisper.
- `GROQ_STT_MODEL = "whisper-large-v3-turbo"` — do not switch to OpenAI Whisper; it hallucinates more on this audio pipeline.
- TTS voice ID and ElevenLabs model are in `.env` / `main.py` constants (`ELEVEN_VOICE_ID`, `ELEVEN_MODEL = "eleven_v3"`).
- WebSocket URL uses `wss://` when page is served over HTTPS.
- Only one in-flight LLM request per WebSocket session; concurrent requests receive `{ type: "busy" }`.
- Weather must use server-side prefetch, not LLM `get_weather` tool — small Groq models fail tool formatting.
## Plan document
See [`PLAN.md`](PLAN.md) for audit findings and implementation status.

55
PLAN.md Normal file
View file

@ -0,0 +1,55 @@
# Plan: Auditoría, reparaciones y nuevas funciones — Arisu AI
## Diagnóstico: problemas encontrados
### Críticos (resueltos)
| Problema | Ubicación | Estado |
|----------|-----------|--------|
| **API keys hardcodeadas en código** | `main.py` | ✅ `.env` + `python-dotenv` |
| **Sin `.gitignore` ni `.env`** | raíz | ✅ |
| **Sin memoria conversacional** | `main.py` | ✅ Historial por sesión WS |
| **Condición de carrera al responder** | `static/app.js` | ✅ `busy` / `cancel` |
### Medios (resueltos)
| Problema | Estado |
|----------|--------|
| Código muerto OpenAI | ✅ Eliminado |
| WebSocket fijo `ws://` | ✅ `wss://` en HTTPS |
| UI monolingüe | ✅ i18n ES/EN |
| Lip-sync sinusoidal | ✅ `AnalyserNode` con TTS |
| Búsquedas con fecha obsoleta | ✅ `APP_TIMEZONE` + enriquecimiento de query |
| Wake word no funciona en Safari | ✅ Audio + `wake_check` en servidor |
| Pérdida de pregunta tras «Arisu» | ✅ Pre-roll, post-buffer, silencio 1700 ms |
| Clima sin datos / error LLM tool | ✅ `weather.py` + prefetch + widget |
| Error Groq `tool_use_failed` | ✅ Retry sin tools + clima fuera del LLM |
## Fases de implementación
1. **Seguridad** — `.env`, `.env.example`, `.gitignore`, `python-dotenv`
2. **Backend** — historial por sesión, cancel/busy, toggle TTS, tool calling
3. **Frontend** — i18n ES/EN, cancelar, lip-sync, limpiar chat, atajos
4. **Documentación** — README.md, CLAUDE.md
5. **Terminal** — `workspace.py` + herramientas LLM
6. **Búsqueda web** — `websearch.py` (Serper / DDG / Wikipedia)
7. **Wake word** — VAD continuo + Whisper en servidor (Safari)
8. **Clima** — Open-Meteo, widget en chat, geolocalización
## Estado
- [x] Fase 1 — Seguridad (.env, .gitignore)
- [x] Fase 2 — Backend (historial, cancel/busy, TTS toggle, tools)
- [x] Fase 3 — Frontend (i18n, lip-sync, limpiar chat, atajos)
- [x] Fase 4 — Documentación (README, CLAUDE.md)
- [x] Modo terminal — workspace + herramientas LLM
- [x] Búsqueda web — Serper / DuckDuckGo / Wikipedia + fecha actual
- [x] Wake word «Arisu» — compatible Safari, buffer post-transcripción
- [x] Clima — widget visual + prefetch server-side (sin tool LLM)
## Posibles mejoras futuras
- Persistencia de historial en MariaDB / PHP (si se integra con stack del usuario)
- Reverse geocoding para mostrar nombre de ciudad con geo del navegador
- Modelo LLM más capaz para tool calling (`llama-3.3-70b` en Groq)
- Modo manos libres con indicador visual más claro durante escucha continua

219
README.md Normal file
View file

@ -0,0 +1,219 @@
# Arisu AI
Asistente de IA con avatar Live2D animado. Responde por voz y texto, te sigue con la mirada a través de la cámara, y habla con voz expresiva usando ElevenLabs.
## Stack
| Capa | Tecnología |
|------|-----------|
| Backend | FastAPI + WebSocket |
| LLM | Groq (`llama-3.1-8b-instant`) |
| STT | Groq Whisper (`whisper-large-v3-turbo`) |
| TTS | ElevenLabs (`eleven_v3`) |
| Clima | Open-Meteo (gratis, sin API key) |
| Avatar | Live2D Cubism 4 + pixi-live2d-display |
| Seguimiento ocular | MediaPipe FaceMesh |
| Frontend | HTML/CSS/JS + Bootstrap 5 + Bootstrap Icons (UI bilingüe ES/EN) |
## Requisitos
- Python 3.9+
- API keys de Groq y ElevenLabs
- Navegador con micrófono (Chrome, Firefox, Safari)
## Instalación
```bash
cd waifu
pip install -r requirements.txt
cp .env.example .env
# Edita .env con tus claves API
python main.py
```
Abrir `http://localhost:8000` en el navegador.
Al primer arranque se descarga automáticamente `live2dcubismcore.min.js` (~202 KB) desde los servidores de Live2D.
> **Seguridad:** Si las claves estuvieron expuestas en código, rótalas en los dashboards de Groq y ElevenLabs.
## Estructura
```
waifu/
├── PLAN.md # Plan de auditoría y mejoras
├── main.py # Backend FastAPI + WebSocket + LLM
├── workspace.py # Herramientas terminal (archivos + shell)
├── websearch.py # Búsqueda web (Serper / DuckDuckGo / Wikipedia)
├── weather.py # Clima Open-Meteo + detección de intención
├── .env.example # Plantilla de variables de entorno
├── requirements.txt
└── static/
├── index.html # UI principal
├── style.css # Tema cyberpunk-anime
├── app.js # WebSocket, audio, chat, wake word, i18n
├── live2d.js # Wrapper Live2D + expresiones + lip-sync
└── live2dcubismcore.min.js # Runtime Cubism 4
```
## Uso
| Acción | Cómo |
|--------|------|
| Activación por voz | Di **«Arisu»** y luego tu pregunta (activo por defecto; ⚙️ para desactivar) |
| Hablar manualmente | Clic en **Hablar**, habla, clic en **Detener** (o barra espaciadora) |
| Escribir | Campo de texto → Enter o botón Enviar |
| Cancelar respuesta | Botón **Detener** o tecla `Escape` |
| Limpiar chat | Icono papelera en el header |
| Activar terminal | ⚙️ → Modo terminal + Directorio base |
| Cambiar modelo Live2D | ⚙️ → Modelo |
| Cambiar cámara/mic | ⚙️ → Cámara / Micrófono |
| Idioma UI | ⚙️ → Idioma UI (Español / English) |
| Idioma voz (STT) | ⚙️ → Idioma voz |
| Activar/desactivar TTS | ⚙️ → Reproducir voz |
### Activación por voz «Arisu»
Compatible con **Safari** (no usa Web Speech API). Flujo:
1. El micrófono escucha continuamente con VAD (detección de voz).
2. Al detectar habla, envía audio al servidor → Whisper busca la palabra «Arisu».
3. Si la frase incluye pregunta tras «Arisu» (*«Arisu, qué clima hace en Madrid»*), se envía directamente.
4. Si solo dice «Arisu», graba tu siguiente frase automáticamente.
El audio que llega mientras el servidor transcribe se guarda en un buffer para no perder la pregunta.
## Clima
Pregunta por el tiempo y verás una **tarjeta visual** en el chat con icono, temperatura, mín/máx, humedad y viento.
Ejemplos:
- *«¿Qué clima hace hoy en Tokio?»*
- *«Arisu, clima en Madrid»*
- *«¿Hace calor hoy?»* (usa geolocalización del navegador si la permites)
El servidor detecta la intención de clima, consulta [Open-Meteo](https://open-meteo.com) y muestra el widget **sin depender del tool-calling del LLM** (evita errores con modelos pequeños).
## Búsqueda web
Arisu puede buscar en internet cuando no sabe algo o necesita datos actuales. El LLM usa la herramienta `web_search` automáticamente.
| Fuente | Cuándo |
|--------|--------|
| **Serper** (Google) | Si configuras `SERPER_API_KEY` en `.env` |
| **DuckDuckGo** | Siempre (gratis) |
| **Wikipedia** | Fallback en el idioma de la sesión |
La fecha actual del sistema se inyecta en el prompt del LLM para consultas de «hoy» o noticias recientes.
Verás en el chat: *Buscando en la web* mientras consulta.
## Modo terminal (workspace)
Arisu puede crear archivos y ejecutar comandos **solo dentro de un directorio base** que tú elijas.
1. Abre ⚙️ **Ajustes**
2. Activa **Modo terminal**
3. Escribe la ruta absoluta, p. ej. `/Users/tu/proyectos/mi-app`
4. Pide por voz o texto: *"Crea un programa Python que sume dos números"*
### Herramientas disponibles (LLM)
| Herramienta | Función |
|-------------|---------|
| `web_search` | Buscar en internet (siempre activa) |
| `list_directory` | Listar archivos y carpetas |
| `read_file` | Leer un archivo de texto |
| `write_file` | Crear o sobrescribir un archivo |
| `create_directory` | Crear carpetas |
| `run_command` | Ejecutar comandos shell en el workspace |
> El clima **no** usa tool-calling del LLM; se resuelve en el servidor vía `weather.py`.
### Seguridad
- Todas las rutas deben estar **dentro** del directorio base (sin `../`)
- Comandos peligrosos bloqueados (`sudo`, `rm -rf /`, etc.)
- Timeout de 45 s por comando
- **Solo úsalo en carpetas de desarrollo**, nunca en `/` o tu home completo
## Memoria conversacional
Cada conexión WebSocket mantiene un historial de hasta 20 mensajes (≈10 turnos). Arisu recuerda el contexto dentro de la sesión. Usa **Limpiar chat** para resetear.
Persistencia: sesión WebSocket + preferencias UI en `localStorage`. No usa base de datos.
## Modelos Live2D disponibles
- **Mao** — chica con uniforme escolar (por defecto)
- **Natori** — chica con traje casual
- **Haru** — chica con vestido
- **Rice** — personaje AR de Live2D Garage
## Audio tags de ElevenLabs
| Tag | Voz | Expresión |
|-----|-----|-----------|
| `[laughs]` | Risa | Cara muy feliz, mejillas rojas |
| `[giggles]` | Risita | Cara feliz, ojos sonrientes |
| `[excited]` | Emocionada | Cara muy expresiva |
| `[whispers]` | Susurro | Expresión calmada |
| `[sighs]` | Suspiro | Expresión neutral |
| `[gasps]` | Sorpresa | Expresión sorprendida |
## Pipeline de audio
```
Micrófono → ScriptProcessor → Float32 → Downsample 16 kHz → Int16 PCM
→ WebSocket binario → WAV → Groq Whisper → texto
→ Groq LLM (streaming + historial + herramientas) → chunks con tags
→ ElevenLabs TTS (opcional) → MP3 base64
→ AudioContext + AnalyserNode → lip-sync Live2D
```
## Mensajes WebSocket
| Dirección | Tipo | Significado |
|-----------|------|---------------|
| cliente→servidor | `audio_config` | Idioma STT, sample_rate, tts_enabled |
| cliente→servidor | `geo_config` | Lat/lon del navegador (clima local) |
| cliente→servidor | `question` | Texto del usuario |
| cliente→servidor | `wake_check` | Audio PCM base64 para detectar «Arisu» |
| cliente→servidor | `workspace_config` | Activar terminal + ruta base |
| cliente→servidor | `cancel` | Cancelar respuesta en curso |
| cliente→servidor | `clear_history` | Borrar historial de sesión |
| cliente→servidor | binario | PCM-16LE a 16 kHz (grabación manual) |
| servidor→cliente | `transcript` | Resultado Whisper |
| servidor→cliente | `wake_ok` | «Arisu» detectado (`text` opcional = pregunta incluida) |
| servidor→cliente | `no_wake` | No se detectó «Arisu» |
| servidor→cliente | `thinking` | LLM iniciado |
| servidor→cliente | `chunk` | Delta streaming |
| servidor→cliente | `done` | LLM terminado |
| servidor→cliente | `cancelled` | Respuesta cancelada |
| servidor→cliente | `busy` | Servidor ocupado |
| servidor→cliente | `tts_audio` | MP3 en base64 |
| servidor→cliente | `tts_error` | ElevenLabs falló |
| servidor→cliente | `no_speech` | Audio demasiado corto o silencioso |
| servidor→cliente | `tool_event` | Herramienta en ejecución |
| servidor→cliente | `tool_result` | Resultado de herramienta |
| servidor→cliente | `weather_widget` | Datos de clima para tarjeta visual |
| servidor→cliente | `workspace_ok` | Workspace validado |
| servidor→cliente | `workspace_error` | Workspace inválido |
| servidor→cliente | `history_cleared` | Historial borrado |
| servidor→cliente | `error` | Error genérico |
## Variables de entorno
```env
GROQ_API_KEY=gsk_...
ELEVENLABS_API_KEY=sk_...
# Opcional
SERPER_API_KEY= # Búsqueda Google vía Serper
APP_TIMEZONE=America/Mexico_City # Zona horaria para «hoy»
GROQ_MODEL=llama-3.1-8b-instant
GROQ_STT_MODEL=whisper-large-v3-turbo
ELEVEN_VOICE_ID=B8gJV1IhpuegLxdpXFOE
ELEVEN_MODEL=eleven_v3
```

547
main.py Normal file
View file

@ -0,0 +1,547 @@
import os
import io
import json
import asyncio
import base64
import re
import urllib.request
import wave
import requests as http_requests
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field
from dotenv import load_dotenv
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from groq import AsyncGroq, Groq as SyncGroq, BadRequestError
from workspace import (
TERMINAL_PROMPT,
TERMINAL_TOOLS,
execute_tool as execute_workspace_tool,
normalize_workspace,
)
from websearch import WEB_PROMPT, WEB_SEARCH_TOOL, web_search, current_date_context
from weather import WEATHER_PROMPT, get_weather, parse_weather_request
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY", "")
if not GROQ_API_KEY or not ELEVENLABS_API_KEY:
raise RuntimeError(
"Faltan GROQ_API_KEY y/o ELEVENLABS_API_KEY. "
"Copia .env.example a .env y configura tus claves."
)
GROQ_MODEL = os.getenv("GROQ_MODEL", "llama-3.1-8b-instant")
GROQ_STT_MODEL = os.getenv("GROQ_STT_MODEL", "whisper-large-v3-turbo")
ELEVEN_VOICE_ID = os.getenv("ELEVEN_VOICE_ID", "B8gJV1IhpuegLxdpXFOE")
ELEVEN_MODEL = os.getenv("ELEVEN_MODEL", "eleven_v3")
MAX_HISTORY = 20
MAX_TOOL_ROUNDS = 10
WAKE_WORD_RE = re.compile(r"\b(arisu|ariso|alice|alisa)\b|アリス", re.I)
SYSTEM_PROMPT = """Eres Arisu, una chica anime amigable, kawaii y muy expresiva.
Respondes SIEMPRE en el mismo idioma que te hable el usuario.
Tus respuestas son breves (máximo 2-3 oraciones), llenas de personalidad y calidez.
Usas ocasionalmente expresiones anime como uwu owo ✨ ~
Eres curiosa, alegre, inteligente y siempre tratas de ser genuinamente útil.
IMPORTANTE — Etiquetas de audio ElevenLabs v3:
Incluye 1 o 2 de estas etiquetas exactas dentro del texto para sonar más expresiva.
SOLO usa estas etiquetas válidas, ninguna otra:
[laughs] [giggles] [excited] [whispers] [sighs] [gasps]
Ponlas siempre rodeadas de espacios, ejemplo:
"¡Hola! [giggles] Me alegra mucho verte por aquí ✨ ¿En qué puedo ayudarte hoy?"
"""
groq_async = AsyncGroq(api_key=GROQ_API_KEY)
groq_sync = SyncGroq(api_key=GROQ_API_KEY)
executor = ThreadPoolExecutor(max_workers=4)
_CUBISM_PATH = os.path.join("static", "live2dcubismcore.min.js")
_CUBISM_URL = "https://cubism.live2d.com/sdk-web/cubismcore/live2dcubismcore.min.js"
def _ensure_assets():
if not (os.path.exists(_CUBISM_PATH) and os.path.getsize(_CUBISM_PATH) > 10_000):
print("Descargando Cubism Core SDK...")
try:
urllib.request.urlretrieve(_CUBISM_URL, _CUBISM_PATH)
print(f" ✓ live2dcubismcore.min.js ({os.path.getsize(_CUBISM_PATH)//1024} KB)")
except Exception as e:
print(f" ✗ {e}")
_ensure_assets()
app = FastAPI()
@dataclass
class SessionState:
lang: str = "es"
sample_rate: int = 16000
tts_enabled: bool = True
terminal_enabled: bool = False
workspace_path: str | None = None
history: list = field(default_factory=list)
is_busy: bool = False
cancelled: bool = False
geo_lat: float | None = None
geo_lon: float | None = None
def _strip_wake_word(text: str) -> str:
m = WAKE_WORD_RE.search(text or "")
if not m:
return (text or "").strip()
remainder = (text or "")[m.end():].strip()
return remainder.lstrip(".,:!?- ")
async def _handle_wake_check(websocket: WebSocket, session: SessionState, pcm_b64: str):
try:
pcm = base64.b64decode(pcm_b64)
except Exception:
await websocket.send_text(json.dumps({"type": "no_wake"}))
return
if len(pcm) < 8_000:
await websocket.send_text(json.dumps({"type": "no_wake"}))
return
try:
wav = pcm16_to_wav(pcm, 16000)
text = await transcribe(wav, session.lang)
except Exception as e:
print(f"Wake check STT error: {e}")
await websocket.send_text(json.dumps({"type": "no_wake"}))
return
if not text or not WAKE_WORD_RE.search(text):
await websocket.send_text(json.dumps({"type": "no_wake"}))
return
remainder = _strip_wake_word(text)
payload: dict = {"type": "wake_ok"}
if remainder and len(remainder) > 1:
payload["text"] = remainder
await websocket.send_text(json.dumps(payload))
def pcm16_to_wav(pcm_bytes: bytes, sample_rate: int) -> bytes:
buf = io.BytesIO()
with wave.open(buf, "wb") as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(pcm_bytes)
return buf.getvalue()
def _trim_history(history: list) -> None:
while len(history) > MAX_HISTORY:
history.pop(0)
def _tools_for_session(session: SessionState) -> list:
tools = [WEB_SEARCH_TOOL]
if session.terminal_enabled and session.workspace_path:
tools.extend(TERMINAL_TOOLS)
return tools
def _system_prompt(session: SessionState) -> str:
prompt = (
SYSTEM_PROMPT + "\n\n" + current_date_context(session.lang)
+ "\n" + WEB_PROMPT + "\n" + WEATHER_PROMPT
)
if session.terminal_enabled and session.workspace_path:
prompt += "\n" + TERMINAL_PROMPT.format(workspace=session.workspace_path)
return prompt
def _assistant_dict(message) -> dict:
d: dict = {"role": "assistant", "content": message.content or ""}
if message.tool_calls:
d["tool_calls"] = [
{
"id": tc.id,
"type": "function",
"function": {
"name": tc.function.name,
"arguments": tc.function.arguments,
},
}
for tc in message.tool_calls
]
return d
def _execute_tool_sync(name: str, args: dict, session: SessionState) -> str | dict:
if name == "web_search":
return web_search(args.get("query", ""), session.lang)
if name == "get_weather":
return get_weather(
args.get("location", ""),
session.lang,
session.geo_lat,
session.geo_lon,
)
if session.terminal_enabled and session.workspace_path:
return execute_workspace_tool(name, args, session.workspace_path)
return f"Error: herramienta '{name}' no disponible"
async def _run_tool(name: str, raw_args: str, session: SessionState) -> tuple[str, dict | None]:
try:
args = json.loads(raw_args) if raw_args else {}
except json.JSONDecodeError as e:
return f"Error: JSON inválido en argumentos — {e}", None
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(
executor, _execute_tool_sync, name, args, session
)
if isinstance(result, dict):
return result.get("text", ""), result.get("widget")
return str(result), None
async def _emit_tool_result(
websocket: WebSocket,
name: str,
args: str,
session: SessionState,
) -> tuple[str, dict | None]:
await websocket.send_text(json.dumps({
"type": "tool_event",
"tool": name,
"args": args,
}))
result, widget = await _run_tool(name, args, session)
if widget:
await websocket.send_text(json.dumps({
"type": "weather_widget",
"data": widget,
}))
preview = result if len(result) <= 400 else result[:400] + "…"
await websocket.send_text(json.dumps({
"type": "tool_result",
"tool": name,
"preview": preview,
}))
return result, widget
async def _prefetch_weather(
websocket: WebSocket, session: SessionState, user_text: str
) -> str | None:
is_weather, location = parse_weather_request(user_text)
if not is_weather:
return None
args = json.dumps({"location": location}, ensure_ascii=False)
result, _ = await _emit_tool_result(websocket, "get_weather", args, session)
return result
async def _llm_completion(messages: list, tools: list | None):
kwargs = {
"model": GROQ_MODEL,
"messages": messages,
"max_tokens": 2048,
}
if tools:
kwargs["tools"] = tools
kwargs["tool_choice"] = "auto"
try:
return await groq_async.chat.completions.create(**kwargs)
except BadRequestError as e:
err = {}
if isinstance(getattr(e, "body", None), dict):
err = e.body.get("error", {})
elif isinstance(getattr(e, "response", None), object):
try:
err = e.response.json().get("error", {})
except Exception:
pass
if err.get("code") != "tool_use_failed":
raise
print(
f"Tool call failed ({GROQ_MODEL}), retrying without tools: "
f"{str(err.get('failed_generation', ''))[:120]}"
)
return await groq_async.chat.completions.create(
model=GROQ_MODEL,
messages=messages,
max_tokens=2048,
)
async def _emit_chunks(websocket: WebSocket, text: str, session: SessionState) -> None:
if not text:
return
step = 12
for i in range(0, len(text), step):
if session.cancelled:
return
await websocket.send_text(json.dumps({"type": "chunk", "text": text[i:i + step]}))
async def _handle_with_tools(
websocket: WebSocket, user_text: str, session: SessionState
) -> str:
messages = [{"role": "system", "content": _system_prompt(session)}]
messages.extend(session.history)
messages.append({"role": "user", "content": user_text})
weather_data = await _prefetch_weather(websocket, session, user_text)
if weather_data:
messages.append({
"role": "system",
"content": (
"DATOS DE CLIMA (ya consultados — responde con esta información; "
"NO uses web_search ni otras herramientas para el clima):\n"
+ weather_data
),
})
tools = _tools_for_session(session)
full_response = ""
for _ in range(MAX_TOOL_ROUNDS):
if session.cancelled:
return full_response
response = await _llm_completion(messages, tools)
msg = response.choices[0].message
if msg.tool_calls:
messages.append(_assistant_dict(msg))
for tc in msg.tool_calls:
if session.cancelled:
return full_response
name = tc.function.name
detail = tc.function.arguments
result, _ = await _emit_tool_result(websocket, name, detail, session)
messages.append({
"role": "tool",
"tool_call_id": tc.id,
"content": result,
})
continue
full_response = msg.content or ""
await _emit_chunks(websocket, full_response, session)
break
return full_response
def _transcribe_sync(wav_bytes: bytes, lang: str) -> str:
audio_buf = io.BytesIO(wav_bytes)
audio_buf.name = "audio.wav"
lang_code = lang[:2].lower()
result = groq_sync.audio.transcriptions.create(
model=GROQ_STT_MODEL,
file=audio_buf,
language=lang_code,
response_format="text",
)
text = result if isinstance(result, str) else result.text
return text.strip()
async def transcribe(wav_bytes: bytes, lang: str) -> str:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(executor, _transcribe_sync, wav_bytes, lang)
def _tts_sync(text: str) -> bytes:
url = f"https://api.elevenlabs.io/v1/text-to-speech/{ELEVEN_VOICE_ID}"
resp = http_requests.post(
url,
json={
"text": text,
"model_id": ELEVEN_MODEL,
"voice_settings": {
"stability": 0.50,
"similarity_boost": 0.75,
"style": 0.0,
"use_speaker_boost": True,
},
},
headers={
"xi-api-key": ELEVENLABS_API_KEY,
"Content-Type": "application/json",
"Accept": "audio/mpeg",
},
timeout=30,
)
if not resp.ok:
print(f" ✗ ElevenLabs {resp.status_code}: {resp.text[:300]}")
resp.raise_for_status()
content_type = resp.headers.get("content-type", "")
if "audio" not in content_type:
raise ValueError(f"ElevenLabs devolvió {content_type} en lugar de audio")
return resp.content
async def tts(text: str) -> bytes:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(executor, _tts_sync, text)
async def _handle_question(websocket: WebSocket, user_text: str, session: SessionState):
if session.is_busy:
await websocket.send_text(json.dumps({"type": "busy"}))
return
session.is_busy = True
session.cancelled = False
await websocket.send_text(json.dumps({"type": "thinking"}))
full_response = ""
try:
full_response = await _handle_with_tools(websocket, user_text, session)
if session.cancelled:
await websocket.send_text(json.dumps({"type": "cancelled"}))
return
await websocket.send_text(json.dumps({"type": "done"}))
if full_response.strip():
session.history.append({"role": "user", "content": user_text})
session.history.append({"role": "assistant", "content": full_response})
_trim_history(session.history)
if session.tts_enabled:
try:
mp3_bytes = await tts(full_response)
if session.cancelled:
return
b64 = base64.b64encode(mp3_bytes).decode("utf-8")
await websocket.send_text(json.dumps({"type": "tts_audio", "data": b64}))
except Exception as e:
print(f"TTS error: {e}")
await websocket.send_text(json.dumps({"type": "tts_error", "text": str(e)}))
except Exception as e:
if not session.cancelled:
await websocket.send_text(json.dumps({"type": "error", "text": f"LLM: {e}"}))
finally:
session.is_busy = False
async def _set_workspace(websocket: WebSocket, session: SessionState, msg: dict):
enabled = bool(msg.get("enabled", False))
path = (msg.get("path") or "").strip()
if not enabled:
session.terminal_enabled = False
session.workspace_path = None
await websocket.send_text(json.dumps({"type": "workspace_ok", "enabled": False}))
return
try:
session.workspace_path = normalize_workspace(path)
session.terminal_enabled = True
await websocket.send_text(json.dumps({
"type": "workspace_ok",
"enabled": True,
"path": session.workspace_path,
}))
except Exception as e:
session.terminal_enabled = False
session.workspace_path = None
await websocket.send_text(json.dumps({"type": "workspace_error", "text": str(e)}))
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
session = SessionState()
try:
while True:
data = await websocket.receive()
if "text" in data:
msg = json.loads(data["text"])
t = msg.get("type")
if t == "audio_config":
session.lang = msg.get("lang", "es-ES")[:2]
session.sample_rate = msg.get("sample_rate", 16000)
if "tts_enabled" in msg:
session.tts_enabled = bool(msg.get("tts_enabled", True))
elif t == "workspace_config":
await _set_workspace(websocket, session, msg)
elif t == "geo_config":
try:
session.geo_lat = float(msg["lat"])
session.geo_lon = float(msg["lon"])
except (KeyError, TypeError, ValueError):
pass
elif t == "cancel":
session.cancelled = True
elif t == "clear_history":
session.history = []
await websocket.send_text(json.dumps({"type": "history_cleared"}))
elif t == "wake_check":
await _handle_wake_check(websocket, session, msg.get("pcm", ""))
elif t == "question":
user_text = msg.get("text", "").strip()
if user_text:
await _handle_question(websocket, user_text, session)
elif "bytes" in data:
if session.is_busy:
await websocket.send_text(json.dumps({"type": "busy"}))
continue
pcm = data["bytes"]
if len(pcm) < 32_000:
await websocket.send_text(json.dumps({"type": "no_speech"}))
continue
try:
wav = pcm16_to_wav(pcm, 16000)
text = await transcribe(wav, session.lang)
if text:
await websocket.send_text(json.dumps({"type": "transcript", "text": text}))
else:
await websocket.send_text(json.dumps({"type": "no_speech"}))
except Exception as e:
print(f"STT error: {e}")
await websocket.send_text(json.dumps({"type": "error", "text": f"STT: {e}"}))
except WebSocketDisconnect:
pass
except Exception as e:
try:
await websocket.send_text(json.dumps({"type": "error", "text": str(e)}))
except Exception:
pass
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
async def root():
return FileResponse("static/index.html")
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

7
requirements.txt Normal file
View file

@ -0,0 +1,7 @@
fastapi
uvicorn[standard]
groq
requests
python-dotenv
duckduckgo-search
websockets

1409
static/app.js Normal file

File diff suppressed because it is too large Load diff

234
static/index.html Normal file
View file

@ -0,0 +1,234 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Arisu AI ✨</title>
<!-- Bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- Bootstrap Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet" />
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800&family=Orbitron:wght@700;900&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="/static/style.css?v=3" />
</head>
<body>
<!-- Animated background orbs -->
<div class="bg-orbs" aria-hidden="true">
<div class="orb orb-1"></div>
<div class="orb orb-2"></div>
<div class="orb orb-3"></div>
</div>
<div id="app">
<!-- ── Header ──────────────────────────────────────────── -->
<header class="app-header">
<div class="brand">
<span class="logo">ARISU</span>
<span class="logo-jp">アリス・エーアイ</span>
</div>
<div class="header-center">
<div class="status-badge">
<div id="statusDot"></div>
<span id="statusTxt">conectando...</span>
</div>
<span id="terminalBadge" class="terminal-badge" data-i18n-title="terminalActiveTitle" title="Modo terminal activo">
<i class="bi bi-terminal-fill"></i>
<span id="terminalBadgeTxt">Terminal</span>
</span>
</div>
<div class="header-actions">
<button id="clearChatBtn" class="icon-btn" data-i18n-title="clearChat" title="Limpiar chat">
<i class="bi bi-trash3"></i>
</button>
<button id="settingsBtn" class="icon-btn" data-i18n-title="settings" title="Ajustes">
<i class="bi bi-sliders2"></i>
</button>
</div>
</header>
<!-- ── Settings panel ──────────────────────────────────── -->
<div id="settingsPanel" class="settings-panel hidden">
<div class="settings-inner">
<aside class="settings-card settings-card-terminal">
<h3 class="settings-card-title">
<i class="bi bi-terminal-fill"></i>
<span id="lblTerminalSection">Modo terminal</span>
</h3>
<label class="settings-toggle" for="terminalToggle">
<input type="checkbox" id="terminalToggle" class="form-check-input" />
<span id="lblTerminalEnabled">Activar modo terminal</span>
</label>
<div class="settings-field settings-field-stack">
<label for="workspaceInput" id="lblWorkspace">
<i class="bi bi-folder2-open"></i> Directorio base
</label>
<input id="workspaceInput" type="text" class="form-control form-control-sm"
placeholder="/Users/tu/proyectos" spellcheck="false" />
</div>
<p id="workspaceStatus" class="workspace-status">Terminal desactivado</p>
</aside>
<section class="settings-card settings-card-general">
<h3 class="settings-card-title">
<i class="bi bi-sliders2"></i>
<span id="lblGeneralSection">General</span>
</h3>
<div class="settings-grid">
<div class="settings-field">
<label for="modelSelect" id="lblModel"><i class="bi bi-person-bounding-box"></i> Modelo</label>
<select id="modelSelect" class="form-select form-select-sm">
<option value="mao">Mao</option>
<option value="natori">Natori</option>
<option value="haru">Haru</option>
<option value="rice">Rice</option>
</select>
</div>
<div class="settings-field">
<label for="camSelect" id="lblCamera"><i class="bi bi-camera-video-fill"></i> Cámara</label>
<select id="camSelect" class="form-select form-select-sm"><option>Cargando...</option></select>
</div>
<div class="settings-field">
<label for="micSelect" id="lblMic"><i class="bi bi-mic-fill"></i> Micrófono</label>
<select id="micSelect" class="form-select form-select-sm"><option>Cargando...</option></select>
</div>
<div class="settings-field">
<label for="uiLangSelect" id="lblUiLang"><i class="bi bi-translate"></i> Idioma UI</label>
<select id="uiLangSelect" class="form-select form-select-sm">
<option value="es">🇪🇸 Español</option>
<option value="en">🇺🇸 English</option>
</select>
</div>
<div class="settings-field">
<label for="langSelect" id="lblSttLang"><i class="bi bi-globe2"></i> Idioma voz</label>
<select id="langSelect" class="form-select form-select-sm">
<option value="es-ES">🇪🇸 Español</option>
<option value="en-US">🇺🇸 English</option>
<option value="ja-JP">🇯🇵 日本語</option>
<option value="fr-FR">🇫🇷 Français</option>
<option value="de-DE">🇩🇪 Deutsch</option>
<option value="pt-BR">🇧🇷 Português</option>
<option value="ko-KR">🇰🇷 한국어</option>
<option value="zh-CN">🇨🇳 中文</option>
</select>
</div>
<div class="settings-field settings-field-check">
<label class="settings-toggle" for="wakeToggle">
<input type="checkbox" id="wakeToggle" class="form-check-input" checked />
<span id="lblWakeEnabled">Activar con «Arisu»</span>
</label>
</div>
<div class="settings-field settings-field-check">
<label class="settings-toggle" for="ttsToggle">
<input type="checkbox" id="ttsToggle" class="form-check-input" checked />
<span id="lblTtsEnabled">Reproducir voz</span>
</label>
</div>
</div>
</section>
</div>
</div>
<!-- ── Main ────────────────────────────────────────────── -->
<main>
<!-- Avatar panel -->
<div class="avatar-panel">
<!-- Loading overlay -->
<div id="loadingOverlay" class="loading-overlay">
<div class="loading-spinner"></div>
<span id="loadingTxt">Cargando modelo Live2D...</span>
</div>
<!-- Decorative corner glyphs -->
<span class="deco deco-tl" aria-hidden="true">✦</span>
<span class="deco deco-tr" aria-hidden="true">✦</span>
<span class="deco deco-bl" aria-hidden="true">❋</span>
<span class="deco deco-br" aria-hidden="true">✧</span>
<canvas id="avatarCanvas" width="644" height="784"></canvas>
<!-- Webcam preview -->
<div class="webcam-box">
<video id="webcamVideo" playsinline muted></video>
<div class="webcam-footer">
<i class="bi bi-eye-fill"></i>
<span id="faceStatus">iniciando...</span>
</div>
</div>
<!-- Avatar name tag -->
<div class="avatar-tag">
<span class="avatar-tag-dot"></span>
Arisu
</div>
</div>
<!-- Chat panel -->
<div class="chat-panel">
<!-- Messages -->
<div id="chatMessages">
<div class="welcome" id="welcomeMsg">
<div class="welcome-glyph">✨</div>
<strong data-i18n="welcomeTitle">¡Hola! Soy Arisu</strong>
<p data-i18n-html="welcomeBody">Pulsa <span class="kbd-badge">🎙️ Hablar</span> y di algo,<br/>
o escribe directamente.<br/>
Te sigo con la mirada (◕‿◕)♡</p>
</div>
</div>
<!-- Audio visualizer -->
<div class="audio-section">
<div class="audio-header">
<div class="mic-dot" id="micDot"></div>
<span id="audioLabel">Audio</span>
<span class="transcript-inline" id="transcript"></span>
</div>
<canvas id="audioCanvas" width="600" height="44"></canvas>
</div>
<!-- Input area -->
<div class="input-area">
<button id="stopBtn" class="stop-btn hidden" title="Detener">
<i class="bi bi-stop-fill"></i>
<span data-i18n="stop">Detener</span>
</button>
<button id="micBtn" class="mic-btn" title="Hablar">
<i class="bi bi-mic-fill" id="micIcon"></i>
<span id="micLabel">Hablar</span>
</button>
<div class="input-wrap">
<input id="textInput" type="text" placeholder="Escríbele algo a Arisu..." autocomplete="off" />
</div>
<button id="sendBtn" class="send-btn" title="Enviar">
<i class="bi bi-send-fill"></i>
</button>
</div>
</div><!-- /chat-panel -->
</main>
</div>
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="/static/live2dcubismcore.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pixi.js@6.5.10/dist/browser/pixi.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pixi-live2d-display@0.4.0/dist/cubism4.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils@0.3/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh@0.4/face_mesh.js" crossorigin="anonymous"></script>
<script src="/static/live2d.js?v=2"></script>
<script src="/static/app.js?v=7"></script>
</body>
</html>

283
static/live2d.js Normal file
View file

@ -0,0 +1,283 @@
// =========================================================
// live2d.js — Live2D Cubism 4 (pixi-live2d-display@0.4.0)
// Requiere: live2dcubismcore.min.js + cubism4.min.js
// =========================================================
const MODELS = {
mao: {
name: "Mao",
url: "https://cdn.jsdelivr.net/gh/Live2D/CubismWebSamples@develop/Samples/Resources/Mao/Mao.model3.json",
},
natori: {
name: "Natori",
url: "https://cdn.jsdelivr.net/gh/Live2D/CubismWebSamples@develop/Samples/Resources/Natori/Natori.model3.json",
},
haru: {
name: "Haru",
url: "https://cdn.jsdelivr.net/gh/Live2D/CubismWebSamples@develop/Samples/Resources/Haru/Haru.model3.json",
},
rice: {
name: "Rice",
url: "https://cdn.jsdelivr.net/gh/Live2D-Garage/CubismWebARSample@master/assets/models/Rice/Rice.model3.json",
},
};
// IDs de parámetro estándar Cubism 4
const PARAM = {
EYE_BALL_X: "ParamEyeBallX",
EYE_BALL_Y: "ParamEyeBallY",
ANGLE_X: "ParamAngleX",
ANGLE_Y: "ParamAngleY",
ANGLE_Z: "ParamAngleZ",
MOUTH_OPEN: "ParamMouthOpenY",
MOUTH_FORM: "ParamMouthForm",
CHEEK: "ParamCheek",
EYE_L_SMILE: "ParamEyeLSmile",
EYE_R_SMILE: "ParamEyeRSmile",
BODY_X: "ParamBodyAngleX",
BREATH: "ParamBreath",
};
// Mapeo de audio tags ElevenLabs → configuración de expresión
const EXPR_MAP = {
laughs: { happy: 1.0, cheek: 0.8, eyeSmile: 0.9, duration: 3500 },
giggles: { happy: 0.8, cheek: 0.6, eyeSmile: 0.7, duration: 2500 },
excited: { happy: 0.95, cheek: 0.5, eyeSmile: 0.8, duration: 3000 },
whispers: { happy: 0.15, cheek: 0.0, eyeSmile: 0.0, duration: 2000 },
sighs: { happy: 0.05, cheek: 0.0, eyeSmile: 0.0, duration: 2000 },
gasps: { happy: 0.4, cheek: 0.2, eyeSmile: 0.1, duration: 1800 },
crying: { happy: 0.0, cheek: 0.3, eyeSmile: 0.0, duration: 3000 },
sarcastic:{ happy: 0.3, cheek: 0.1, eyeSmile: 0.2, duration: 2000 },
};
class Live2DAvatar {
constructor(canvas) {
this.canvas = canvas;
this.W = canvas.width;
this.H = canvas.height;
this.app = null;
this.model = null;
this.loaded = false;
// Gaze
this.lookX = 0;
this.lookY = 0;
this.targetX = 0;
this.targetY = 0;
this.tracking = false;
// Idle
this.idleTimer = 0;
this.idleX = 0;
this.idleY = 0;
// Mouth
this.speaking = false;
this.ttsAnalyser = null;
this.mouthOpen = 0;
this.mouthPhase = 0;
// Expression
this.happy = 0;
this.targetHappy = 0;
this.exprCheek = 0;
this.exprEyeSmile = 0;
this._exprTimer = null;
this._lastMs = 0;
this._tickBound = this._tick.bind(this);
}
// ── Public API ──────────────────────────────────────────
setLook(x, y) {
this.targetX = Math.max(-1, Math.min(1, x));
this.targetY = Math.max(-1, Math.min(1, y));
this.tracking = true;
this.idleTimer = 2000;
}
clearLook() { this.tracking = false; }
startSpeaking(analyser = null) {
this.speaking = true;
this.ttsAnalyser = analyser;
if (this.targetHappy < 0.5) this.targetHappy = 0.55;
}
stopSpeaking() {
this.speaking = false;
this.ttsAnalyser = null;
this.targetHappy = 0;
}
setHappy(v) { this.targetHappy = Math.max(0, Math.min(1, v)); }
start() {}
stop() { this.app?.destroy(true); }
// ── Expresión por audio tag de ElevenLabs ───────────────
setExpression(tag) {
const cfg = EXPR_MAP[tag.toLowerCase()];
if (!cfg) return;
clearTimeout(this._exprTimer);
this.targetHappy = cfg.happy;
this.exprCheek = cfg.cheek;
this.exprEyeSmile = cfg.eyeSmile;
// Intentar reproducir motion del modelo si está disponible
if (this.model) {
try { this.model.motion("TapBody"); } catch (_) {}
}
this._exprTimer = setTimeout(() => {
this.exprCheek = 0;
this.exprEyeSmile = 0;
this.targetHappy = this.speaking ? 0.55 : 0;
}, cfg.duration);
}
// ── Load ────────────────────────────────────────────────
async load(modelKey = "mao") {
if (!this.app) {
this.app = new PIXI.Application({
view: this.canvas,
autoStart: true,
backgroundAlpha: 0,
width: this.W,
height: this.H,
antialias: true,
});
PIXI.live2d.Live2DModel.registerTicker(PIXI.Ticker);
}
const entry = MODELS[modelKey] || MODELS.mao;
console.log(`Cargando modelo: ${entry.name} — ${entry.url}`);
const model = await PIXI.live2d.Live2DModel.from(entry.url, {
autoInteract: false,
});
this.model = model;
this.app.stage.addChild(model);
// Escalar grande — ancla en la esquina superior, cara visible
model.anchor.set(0.5, 0); // ancla en el borde superior del modelo
const scaleX = (this.W * 1.9) / model.internalModel.originalWidth;
const scaleY = (this.H * 1.8) / model.internalModel.originalHeight;
model.scale.set(Math.min(scaleX, scaleY));
model.x = this.W / 2;
// La cara está en el ~15% superior del modelo: empujar hacia abajo
// para que se vea cara + torso y el cuerpo inferior quede recortado
const modelDispH = model.scale.y * model.internalModel.originalHeight;
model.y = -(modelDispH * 0.08); // 8% del modelo sobre el borde superior
this.loaded = true;
this._lastMs = performance.now();
this.app.ticker.add(this._tickBound, null, PIXI.UPDATE_PRIORITY.LOW);
return model;
}
async switchModel(modelKey) {
this.app.ticker.remove(this._tickBound);
if (this.model) {
this.app.stage.removeChild(this.model);
this.model.destroy();
this.model = null;
this.loaded = false;
}
await this.load(modelKey);
}
// ── Tick ────────────────────────────────────────────────
_tick() {
if (!this.loaded || !this.model) return;
const now = performance.now();
const dt = Math.min((now - this._lastMs) / 1000, 0.05);
this._lastMs = now;
// Gaze wander
if (!this.tracking) {
this.idleTimer -= dt * 1000;
if (this.idleTimer <= 0) {
this.idleTimer = 2000 + Math.random() * 3000;
this.idleX = (Math.random() - 0.5) * 0.45;
this.idleY = (Math.random() - 0.5) * 0.28;
}
this.targetX = this.idleX;
this.targetY = this.idleY;
}
const sp = this.tracking ? 0.09 : 0.04;
this.lookX += (this.targetX - this.lookX) * sp;
this.lookY += (this.targetY - this.lookY) * sp;
// Mouth — lip-sync con analyser TTS o fallback sinusoidal
if (this.speaking) {
let level = 0;
if (this.ttsAnalyser) {
const buf = new Uint8Array(this.ttsAnalyser.fftSize);
this.ttsAnalyser.getByteTimeDomainData(buf);
let sum = 0;
for (let i = 0; i < buf.length; i++) {
const v = (buf[i] - 128) / 128;
sum += v * v;
}
level = Math.min(1, Math.sqrt(sum / buf.length) * 5);
}
if (level > 0.04) {
this.mouthOpen = 0.12 + level * 0.78;
} else {
this.mouthPhase += dt * 12;
this.mouthOpen = 0.25 + Math.abs(Math.sin(this.mouthPhase)) * 0.55;
}
} else {
this.mouthOpen *= 0.82;
}
this.happy += (this.targetHappy - this.happy) * 0.05;
// Aplicar al core
try {
const fx = this.W / 2 + this.lookX * this.W * 0.3;
const fy = this.H * 0.28 + this.lookY * this.H * 0.18; // apunta a la cara
this.model.focus(fx, fy);
const core = this.model.internalModel.coreModel;
core.setParameterValueById(PARAM.MOUTH_OPEN, this.mouthOpen);
// Happy base
const h = this.happy;
if (h > 0.02) {
this._tryParam(core, PARAM.MOUTH_FORM, h * 0.6);
}
// Expresión override si está activa
const eyeS = Math.max(h * 0.5, this.exprEyeSmile);
const cheek = Math.max(h * 0.35, this.exprCheek);
if (eyeS > 0.01) {
this._tryParam(core, PARAM.EYE_L_SMILE, eyeS);
this._tryParam(core, PARAM.EYE_R_SMILE, eyeS);
}
if (cheek > 0.01) {
this._tryParam(core, PARAM.CHEEK, cheek);
}
} catch (_) {}
}
_tryParam(core, id, val) {
try { core.setParameterValueById(id, val); } catch (_) {}
}
}
// Poblar el selector
document.addEventListener("DOMContentLoaded", () => {
const sel = document.getElementById("modelSelect");
if (!sel) return;
sel.innerHTML = Object.entries(MODELS)
.map(([k, v]) => `<option value="${k}">${v.name}</option>`)
.join("");
});
window.LIVE2D_MODELS = MODELS;
window.Live2DAvatar = Live2DAvatar;

9
static/live2dcubismcore.min.js vendored Normal file

File diff suppressed because one or more lines are too long

938
static/style.css Normal file
View file

@ -0,0 +1,938 @@
/* ─────────────────────────────────────────────────────────────
Arisu AI — Cyberpunk × Anime theme (blue edition)
Bootstrap 5 is loaded before this file.
───────────────────────────────────────────────────────────── */
/* ── Custom properties ──────────────────────────────────── */
:root {
--bg: #000d1a;
--surface: rgba(255, 255, 255, 0.04);
--surface2: rgba(255, 255, 255, 0.07);
--border: rgba(96, 165, 250, 0.2);
--border-hi: rgba(147, 197, 253, 0.45);
--blue: #60a5fa;
--blue-dark: #3b82f6;
--blue-lite: #93c5fd;
--pink: #f472b6;
--cyan: #22d3ee;
--text: #e8f4ff;
--dim: #7cb9f4;
--font: "Nunito", "Segoe UI", sans-serif;
--font-logo: "Orbitron", monospace;
}
/* ── Reset ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
height: 100%;
background: var(--bg);
color: var(--text);
font-family: var(--font);
overflow: hidden;
}
/* ── Animated background orbs ───────────────────────────── */
.bg-orbs {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 0;
overflow: hidden;
}
.orb {
position: absolute;
border-radius: 50%;
filter: blur(90px);
opacity: 0.16;
animation: orb-drift 22s ease-in-out infinite alternate;
}
.orb-1 {
width: 640px; height: 640px;
background: var(--blue-dark);
top: -220px; left: -160px;
animation-duration: 24s;
}
.orb-2 {
width: 520px; height: 520px;
background: var(--pink);
top: 38%; right: -180px;
animation-duration: 19s;
animation-direction: alternate-reverse;
opacity: 0.12;
}
.orb-3 {
width: 460px; height: 460px;
background: var(--cyan);
bottom: -160px; left: 33%;
animation-duration: 28s;
opacity: 0.1;
}
@keyframes orb-drift {
0% { transform: translate(0,0) scale(1); }
50% { transform: translate(40px,30px) scale(1.07); }
100% { transform: translate(-20px,50px) scale(0.95); }
}
/* ── App layout ─────────────────────────────────────────── */
#app {
position: relative;
z-index: 1;
display: grid;
grid-template-rows: 54px auto 1fr;
height: 100vh;
max-width: 1380px;
margin: 0 auto;
}
/* ── Header ─────────────────────────────────────────────── */
.app-header {
display: flex;
align-items: center;
gap: 16px;
padding: 0 24px;
border-bottom: 1px solid var(--border);
background: rgba(0, 10, 26, 0.88);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.brand {
display: flex;
align-items: baseline;
gap: 10px;
}
.logo {
font-family: var(--font-logo);
font-size: 1.22rem;
font-weight: 900;
letter-spacing: 3px;
background: linear-gradient(90deg, var(--blue-lite), var(--cyan), var(--pink));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.logo-jp {
font-size: 0.64rem;
color: var(--dim);
letter-spacing: 1px;
opacity: 0.65;
}
.header-center { flex: 1; display: flex; justify-content: center; align-items: center; gap: 10px; flex-wrap: wrap; }
.status-badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 4px 12px;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 20px;
font-size: 0.7rem;
color: var(--dim);
}
.terminal-badge {
display: none;
align-items: center;
gap: 5px;
font-size: 0.65rem;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
color: #22d3ee;
background: rgba(34, 211, 238, 0.12);
border: 1px solid rgba(34, 211, 238, 0.35);
border-radius: 999px;
padding: 4px 10px;
}
.terminal-badge.visible { display: inline-flex; }
#statusDot {
width: 7px; height: 7px;
border-radius: 50%;
background: #f59e0b;
box-shadow: 0 0 8px currentColor;
transition: background 0.4s;
}
.header-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
color: var(--dim);
width: 34px; height: 34px;
display: flex; align-items: center; justify-content: center;
cursor: pointer;
font-size: 1rem;
transition: border-color 0.2s, background 0.2s, color 0.2s;
flex-shrink: 0;
}
.icon-btn:hover, .icon-btn.open {
border-color: var(--blue);
background: rgba(96, 165, 250, 0.12);
color: var(--blue-lite);
}
/* ── Settings panel ─────────────────────────────────────── */
.settings-panel {
border-bottom: 1px solid var(--border);
background: rgba(0, 8, 22, 0.95);
backdrop-filter: blur(14px);
overflow-x: hidden;
overflow-y: auto;
max-height: min(58vh, 480px);
opacity: 1;
transition: max-height 0.35s ease, opacity 0.25s ease;
}
.settings-panel.hidden {
max-height: 0;
opacity: 0;
pointer-events: none;
overflow: hidden;
}
.settings-inner {
display: grid;
grid-template-columns: minmax(260px, 300px) 1fr;
gap: 14px;
padding: 14px 20px 18px;
align-items: start;
}
.settings-card {
border-radius: 12px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.03);
padding: 14px 16px;
display: flex;
flex-direction: column;
gap: 12px;
min-width: 0;
}
.settings-card-terminal {
border-color: rgba(34, 211, 238, 0.28);
background: rgba(34, 211, 238, 0.05);
}
.settings-card-title {
margin: 0;
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--cyan);
display: flex;
align-items: center;
gap: 8px;
padding-bottom: 8px;
border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.settings-card-general .settings-card-title {
color: var(--blue-lite);
}
.settings-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px 14px;
}
.settings-field {
display: grid;
grid-template-columns: 88px minmax(0, 1fr);
align-items: center;
gap: 8px;
min-width: 0;
}
.settings-field label {
font-size: 0.72rem;
color: var(--dim);
display: flex;
align-items: center;
gap: 5px;
white-space: nowrap;
margin: 0;
}
.settings-field-stack {
grid-template-columns: 1fr;
gap: 6px;
}
.settings-field-stack label {
font-size: 0.72rem;
color: var(--dim);
}
.settings-field-check {
grid-column: 1 / -1;
grid-template-columns: 1fr;
}
.settings-toggle {
display: inline-flex;
align-items: center;
gap: 8px;
margin: 0;
cursor: pointer;
user-select: none;
font-size: 0.8rem;
color: var(--text);
font-weight: 600;
}
.settings-toggle .form-check-input {
margin: 0;
flex-shrink: 0;
}
.settings-panel .form-select,
.settings-panel .form-control {
background-color: rgba(0, 15, 40, 0.85);
border: 1px solid rgba(96, 165, 250, 0.25);
border-radius: 8px;
color: var(--text);
font-family: var(--font);
font-size: 0.78rem;
min-width: 0;
width: 100%;
transition: border-color 0.2s, box-shadow 0.2s;
outline: none;
}
.settings-panel .form-select {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
padding: 5px 28px 5px 10px;
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%237cb9f4' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: right 0.55rem center;
background-size: 14px 10px;
}
.settings-panel .form-control {
padding: 5px 10px;
}
.settings-panel .form-select:focus,
.settings-panel .form-control:focus {
border-color: var(--blue);
box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.2);
}
.settings-panel .form-select option { background: #000d22; color: var(--text); }
.settings-card-terminal .form-control {
border-color: rgba(34, 211, 238, 0.3);
}
.workspace-status {
margin: 0;
font-size: 0.7rem;
color: var(--dim);
word-break: break-all;
line-height: 1.35;
padding: 6px 8px;
border-radius: 6px;
background: rgba(0, 0, 0, 0.2);
}
.workspace-status.active { color: #4ade80; }
.workspace-status.error { color: #f87171; }
@media (max-width: 960px) {
.settings-inner {
grid-template-columns: 1fr;
}
.settings-grid {
grid-template-columns: 1fr;
}
.settings-field {
grid-template-columns: 96px minmax(0, 1fr);
}
}
/* legacy row helpers — keep for compatibility */
.settings-row {
display: flex;
align-items: center;
gap: 8px;
flex: 1;
min-width: 160px;
}
.settings-row label {
font-size: 0.72rem;
color: var(--dim);
white-space: nowrap;
display: flex;
align-items: center;
gap: 5px;
}
.settings-row.settings-check label {
cursor: pointer;
user-select: none;
}
.settings-row.settings-check .form-check-input {
margin: 0 6px 0 0;
}
.settings-row.settings-full {
flex-direction: column;
align-items: stretch;
min-width: 100%;
}
/* ── Main layout ─────────────────────────────────────────── */
main {
display: grid;
grid-template-columns: 660px 1fr;
overflow: hidden;
}
/* ── Loading overlay ────────────────────────────────────── */
.loading-overlay {
position: absolute;
inset: 0;
z-index: 20;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 14px;
background: rgba(0, 10, 26, 0.92);
backdrop-filter: blur(8px);
color: var(--dim);
font-size: 0.85rem;
}
.loading-spinner {
width: 40px; height: 40px;
border: 2px solid var(--border);
border-top-color: var(--pink);
border-right-color: var(--blue);
border-radius: 50%;
animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* ── Avatar panel ───────────────────────────────────────── */
.avatar-panel {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-right: 1px solid var(--border);
overflow: hidden;
background:
radial-gradient(ellipse 60% 55% at 50% 45%, rgba(59,130,246,0.1) 0%, transparent 75%),
radial-gradient(ellipse 40% 30% at 50% 85%, rgba(244,114,182,0.06) 0%, transparent 70%);
}
/* Scanline texture */
.avatar-panel::before {
content: "";
position: absolute;
inset: 0;
background: repeating-linear-gradient(
0deg, transparent, transparent 3px,
rgba(96,165,250,0.015) 3px, rgba(96,165,250,0.015) 4px
);
pointer-events: none;
z-index: 1;
}
#avatarCanvas {
display: block;
max-width: 100%;
max-height: 100%;
position: relative;
z-index: 2;
filter:
drop-shadow(0 0 28px rgba(96, 165, 250, 0.45))
drop-shadow(0 0 70px rgba(59, 130, 246, 0.2));
}
/* Corner glyphs */
.deco {
position: absolute;
z-index: 3;
color: rgba(147, 197, 253, 0.22);
font-size: 1.1rem;
pointer-events: none;
animation: deco-pulse 4s ease-in-out infinite;
}
.deco-tl { top: 16px; left: 18px; animation-delay: 0s; }
.deco-tr { top: 16px; right: 18px; animation-delay: 1s; }
.deco-bl { bottom: 50px; left: 18px; animation-delay: 2s; }
.deco-br { bottom: 50px; right: 18px; animation-delay: 3s; }
@keyframes deco-pulse {
0%, 100% { opacity: 0.22; transform: scale(1); }
50% { opacity: 0.7; transform: scale(1.2); }
}
/* Webcam */
.webcam-box {
position: absolute;
top: 14px;
left: 14px;
width: 132px;
border-radius: 12px;
overflow: hidden;
border: 1px solid var(--border-hi);
background: #000;
box-shadow: 0 0 18px rgba(96,165,250,0.22), 0 4px 20px rgba(0,0,0,0.7);
z-index: 4;
}
.webcam-box video {
width: 100%;
display: block;
transform: scaleX(-1);
opacity: 0.88;
}
.webcam-footer {
display: flex;
align-items: center;
gap: 5px;
padding: 3px 8px 4px;
background: rgba(0, 8, 22, 0.9);
font-size: 0.58rem;
color: var(--dim);
}
#faceStatus { font-size: 0.58rem; color: #86efac; }
/* Name tag */
.avatar-tag {
position: absolute;
bottom: 16px;
left: 50%;
transform: translateX(-50%);
z-index: 4;
display: inline-flex;
align-items: center;
gap: 7px;
padding: 4px 14px;
background: rgba(0, 8, 22, 0.8);
border: 1px solid var(--border-hi);
border-radius: 20px;
font-size: 0.72rem;
font-weight: 700;
color: var(--dim);
letter-spacing: 1px;
backdrop-filter: blur(10px);
}
.avatar-tag-dot {
width: 6px; height: 6px;
border-radius: 50%;
background: var(--pink);
box-shadow: 0 0 8px var(--pink);
animation: tag-pulse 2s ease-in-out infinite;
}
@keyframes tag-pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.35; }
}
/* ── Chat panel ─────────────────────────────────────────── */
.chat-panel {
display: grid;
grid-template-rows: 1fr auto auto;
overflow: hidden;
background: rgba(0, 8, 20, 0.45);
backdrop-filter: blur(4px);
}
/* Messages */
#chatMessages {
overflow-y: auto;
padding: 20px 18px 10px;
display: flex;
flex-direction: column;
gap: 12px;
scroll-behavior: smooth;
}
#chatMessages::-webkit-scrollbar { width: 4px; }
#chatMessages::-webkit-scrollbar-track { background: transparent; }
#chatMessages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.msg { display: flex; flex-direction: column; gap: 4px; max-width: 82%; }
.msg-user { align-self: flex-end; align-items: flex-end; }
.msg-yuki { align-self: flex-start; align-items: flex-start; }
.msg-label {
font-size: 0.62rem;
color: var(--dim);
padding: 0 6px;
letter-spacing: 0.4px;
opacity: 0.75;
}
.msg-text {
padding: 10px 15px;
border-radius: 18px;
font-size: 0.88rem;
line-height: 1.6;
word-break: break-word;
}
.msg-user .msg-text {
background: linear-gradient(135deg, rgba(37,99,235,0.5), rgba(96,165,250,0.28));
border: 1px solid rgba(96,165,250,0.3);
border-bottom-right-radius: 5px;
box-shadow: 0 2px 16px rgba(59,130,246,0.2);
}
.msg-yuki .msg-text {
background: rgba(244, 114, 182, 0.08);
border: 1px solid rgba(244, 114, 182, 0.16);
border-bottom-left-radius: 5px;
box-shadow: 0 2px 16px rgba(244,114,182,0.07);
}
.msg-system { color: var(--dim); font-size: 0.74rem; font-style: italic; }
.msg-tool {
align-self: stretch;
max-width: 95%;
font-family: ui-monospace, "Cascadia Code", monospace;
font-size: 0.68rem;
color: #93c5fd;
background: rgba(59, 130, 246, 0.08);
border: 1px solid rgba(96, 165, 250, 0.2);
border-radius: 8px;
padding: 6px 10px;
word-break: break-word;
}
.msg-tool .tool-name { color: var(--cyan); font-weight: 700; }
.msg-weather {
align-self: flex-start;
max-width: 92%;
}
.weather-card {
background: linear-gradient(135deg, rgba(56, 189, 248, 0.14), rgba(167, 139, 250, 0.12));
border: 1px solid rgba(125, 211, 252, 0.35);
border-radius: 14px;
padding: 14px 16px;
box-shadow: 0 4px 24px rgba(56, 189, 248, 0.12);
min-width: 240px;
}
.weather-card-header {
display: flex;
align-items: center;
gap: 14px;
}
.weather-icon {
font-size: 2.6rem;
color: #fde047;
filter: drop-shadow(0 0 10px rgba(253, 224, 71, 0.45));
flex-shrink: 0;
}
.weather-main {
display: flex;
flex-direction: column;
gap: 2px;
}
.weather-temp {
font-size: 1.85rem;
font-weight: 800;
line-height: 1;
color: #e0f2fe;
}
.weather-desc {
font-size: 0.88rem;
color: rgba(186, 230, 253, 0.9);
}
.weather-location {
margin-top: 10px;
font-size: 0.82rem;
color: rgba(186, 230, 253, 0.75);
display: flex;
align-items: center;
gap: 6px;
}
.weather-meta {
display: flex;
flex-wrap: wrap;
gap: 10px 14px;
margin-top: 10px;
font-size: 0.76rem;
color: rgba(186, 230, 253, 0.65);
}
.weather-meta span {
display: inline-flex;
align-items: center;
gap: 4px;
}
.weather-date {
margin-top: 8px;
font-size: 0.7rem;
color: rgba(186, 230, 253, 0.45);
text-transform: uppercase;
letter-spacing: 0.04em;
}
/* Thinking dots */
.thinking {
display: flex !important;
align-items: center;
gap: 6px;
padding: 12px 18px !important;
}
.dot {
width: 7px; height: 7px;
border-radius: 50%;
background: var(--pink);
box-shadow: 0 0 8px var(--pink);
animation: bounce 1.3s ease-in-out infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; background: var(--blue); box-shadow: 0 0 8px var(--blue); }
.dot:nth-child(3) { animation-delay: 0.4s; background: var(--cyan); box-shadow: 0 0 8px var(--cyan); }
@keyframes bounce {
0%, 60%, 100% { transform: translateY(0); opacity: 0.55; }
30% { transform: translateY(-8px); opacity: 1; }
}
/* Welcome */
.welcome {
align-self: center;
text-align: center;
padding: 32px 24px;
color: rgba(124, 185, 244, 0.5);
line-height: 1.75;
}
.welcome-glyph {
font-size: 2.2rem;
margin-bottom: 10px;
filter: drop-shadow(0 0 16px rgba(244, 114, 182, 0.6));
animation: welcome-float 3s ease-in-out infinite;
}
@keyframes welcome-float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-6px); }
}
.welcome strong {
display: block;
font-size: 1.1rem;
font-weight: 800;
color: var(--pink);
text-shadow: 0 0 20px rgba(244,114,182,0.4);
margin-bottom: 8px;
}
.welcome p { font-size: 0.84rem; margin: 0; }
.kbd-badge {
display: inline-block;
padding: 2px 8px;
background: var(--surface2);
border: 1px solid var(--border);
border-radius: 6px;
font-size: 0.78rem;
color: var(--dim);
}
/* ── Audio section ──────────────────────────────────────── */
.audio-section {
padding: 8px 16px 6px;
border-top: 1px solid var(--border);
background: rgba(0, 6, 18, 0.55);
}
.audio-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 5px;
font-size: 0.68rem;
color: var(--dim);
}
.mic-dot {
width: 7px; height: 7px;
border-radius: 50%;
background: #4ade80;
flex-shrink: 0;
animation: mic-pulse 2s ease-in-out infinite;
}
@keyframes mic-pulse {
0%, 100% { box-shadow: 0 0 0 0 rgba(74,222,128,0.5); }
50% { box-shadow: 0 0 0 5px rgba(74,222,128,0); }
}
.transcript-inline {
margin-left: auto;
font-size: 0.68rem;
color: var(--blue-lite);
font-style: italic;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 55%;
}
#audioCanvas {
width: 100%;
height: 44px;
display: block;
border-radius: 8px;
background: rgba(0,0,0,0.3);
}
/* ── Input area ─────────────────────────────────────────── */
.input-area {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 16px 14px;
border-top: 1px solid var(--border);
background: rgba(0, 6, 18, 0.7);
}
.mic-btn {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 9px 16px;
border-radius: 24px;
border: 1.5px solid var(--border-hi);
background: rgba(59, 130, 246, 0.14);
color: var(--text);
font-family: var(--font);
font-size: 0.82rem;
font-weight: 700;
cursor: pointer;
white-space: nowrap;
flex-shrink: 0;
transition: background 0.2s, border-color 0.2s, box-shadow 0.2s;
}
.mic-btn:hover {
background: rgba(59, 130, 246, 0.28);
border-color: var(--blue);
box-shadow: 0 0 18px rgba(96,165,250,0.28);
}
.mic-btn.active {
background: rgba(239, 68, 68, 0.2);
border-color: #f87171;
color: #fca5a5;
animation: mic-record-pulse 1s ease-in-out infinite;
}
.mic-btn.wake-listen {
background: rgba(34, 211, 238, 0.12);
border-color: rgba(34, 211, 238, 0.55);
color: #a5f3fc;
animation: mic-wake-pulse 2.2s ease-in-out infinite;
}
@keyframes mic-wake-pulse {
0%, 100% { box-shadow: 0 0 0 0 rgba(34, 211, 238, 0.35); }
50% { box-shadow: 0 0 0 8px rgba(34, 211, 238, 0); }
}
.mic-btn:disabled,
.send-btn:disabled,
.input-wrap input:disabled {
opacity: 0.45;
cursor: not-allowed;
}
@keyframes mic-record-pulse {
0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.4); }
50% { box-shadow: 0 0 0 10px rgba(239,68,68,0); }
}
.stop-btn {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 9px 16px;
border-radius: 24px;
border: 1.5px solid #f87171;
background: rgba(239, 68, 68, 0.22);
color: #fecaca;
font-family: var(--font);
font-size: 0.82rem;
font-weight: 700;
cursor: pointer;
white-space: nowrap;
flex-shrink: 0;
transition: background 0.2s, box-shadow 0.2s;
}
.stop-btn:hover {
background: rgba(239, 68, 68, 0.35);
box-shadow: 0 0 18px rgba(239,68,68,0.25);
}
.stop-btn.hidden { display: none; }
.input-wrap {
flex: 1;
position: relative;
}
.input-wrap::before {
content: "";
position: absolute;
inset: -1px;
border-radius: 25px;
background: linear-gradient(90deg, var(--blue-dark), var(--cyan), var(--pink));
opacity: 0;
transition: opacity 0.25s;
z-index: 0;
}
.input-wrap:focus-within::before { opacity: 1; }
#textInput {
position: relative;
z-index: 1;
width: 100%;
padding: 10px 16px;
border-radius: 24px;
border: 1.5px solid var(--border);
background: rgba(0, 10, 30, 0.75);
color: var(--text);
font-family: var(--font);
font-size: 0.86rem;
outline: none;
transition: border-color 0.2s, background 0.2s;
}
#textInput::placeholder { color: rgba(124,185,244,0.3); }
#textInput:focus {
border-color: transparent;
background: rgba(0, 14, 38, 0.85);
}
.send-btn {
width: 40px; height: 40px;
border-radius: 50%;
border: none;
background: linear-gradient(135deg, var(--blue-dark), var(--cyan));
color: #fff;
font-size: 0.95rem;
display: flex; align-items: center; justify-content: center;
cursor: pointer;
flex-shrink: 0;
transition: transform 0.15s, box-shadow 0.2s, opacity 0.2s;
box-shadow: 0 0 18px rgba(96,165,250,0.4);
}
.send-btn:hover { transform: scale(1.08); box-shadow: 0 0 28px rgba(34,211,238,0.5); }
.send-btn:active { transform: scale(0.95); opacity: 0.85; }
/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 860px) {
html, body { overflow: auto; }
#app {
grid-template-rows: 54px auto 1fr;
height: auto;
min-height: 100vh;
}
main {
grid-template-columns: 1fr;
grid-template-rows: 420px 1fr;
}
.avatar-panel {
height: 420px;
border-right: none;
border-bottom: 1px solid var(--border);
}
#avatarCanvas { height: 420px; }
.settings-inner { gap: 10px; }
.settings-row { min-width: 140px; }
}

250
weather.py Normal file
View file

@ -0,0 +1,250 @@
"""Clima vía Open-Meteo (gratis, sin API key)."""
import json
import re
from datetime import datetime
from zoneinfo import ZoneInfo
import requests as http_requests
TIMEOUT = 12
GEOCODE_URL = "https://geocoding-api.open-meteo.com/v1/search"
FORECAST_URL = "https://api.open-meteo.com/v1/forecast"
# WMO weather codes → icon key + descriptions
_WMO = {
0: ("clear", {"es": "Despejado", "en": "Clear sky"}),
1: ("mostly-clear", {"es": "Mayormente despejado","en": "Mainly clear"}),
2: ("partly-cloudy",{"es": "Parcialmente nublado","en": "Partly cloudy"}),
3: ("cloudy", {"es": "Nublado", "en": "Overcast"}),
45: ("fog", {"es": "Niebla", "en": "Fog"}),
48: ("fog", {"es": "Niebla helada", "en": "Depositing rime fog"}),
51: ("drizzle", {"es": "Llovizna ligera", "en": "Light drizzle"}),
53: ("drizzle", {"es": "Llovizna", "en": "Drizzle"}),
55: ("drizzle", {"es": "Llovizna intensa", "en": "Heavy drizzle"}),
61: ("rain", {"es": "Lluvia ligera", "en": "Light rain"}),
63: ("rain", {"es": "Lluvia", "en": "Rain"}),
65: ("rain-heavy", {"es": "Lluvia fuerte", "en": "Heavy rain"}),
71: ("snow", {"es": "Nieve ligera", "en": "Light snow"}),
73: ("snow", {"es": "Nieve", "en": "Snow"}),
75: ("snow", {"es": "Nieve intensa", "en": "Heavy snow"}),
80: ("showers", {"es": "Chubascos ligeros", "en": "Light showers"}),
81: ("showers", {"es": "Chubascos", "en": "Showers"}),
82: ("showers-heavy",{"es": "Chubascos fuertes", "en": "Heavy showers"}),
95: ("thunder", {"es": "Tormenta", "en": "Thunderstorm"}),
96: ("thunder", {"es": "Tormenta con granizo","en": "Thunderstorm with hail"}),
99: ("thunder", {"es": "Tormenta con granizo","en": "Thunderstorm with hail"}),
}
WEATHER_TOOL = {
"type": "function",
"function": {
"name": "get_weather",
"description": (
"Obtiene el clima ACTUAL y de hoy para una ciudad o ubicación. "
"Úsala cuando pregunten por el tiempo, clima, temperatura, lluvia, "
"hace calor/frío hoy, etc. No uses web_search para clima."
),
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "Ciudad o lugar, ej. 'Madrid', 'Ciudad de México'. Vacío = ubicación del usuario.",
},
},
"required": [],
},
},
}
WEATHER_PROMPT = """
CLIMA:
Si el system prompt incluye «DATOS DE CLIMA», responde con esos datos de forma natural.
No uses web_search para preguntas de clima/tiempo/temperatura.
"""
_WEATHER_WORDS = re.compile(
r"\b("
r"clima|tiempo|temperatura|lluvia|llover|lloviendo|llovizna|"
r"calor|fr[ií]o|nublado|soleado|tormenta|"
r"weather|forecast|rain|raining|snow|hot|cold|humidity|wind"
r")\b",
re.I,
)
_LOC_PATTERNS = (
re.compile(
r"(?:clima|tiempo|temperatura|weather|forecast)\s+(?:en|de|del|la|in|for|at)\s+"
r"(.+?)(?:\?|[.!]|$)",
re.I,
),
re.compile(
r"(?:en|in|for|at)\s+(.+?)\s+(?:clima|tiempo|weather|forecast)",
re.I,
),
re.compile(
r"(?:qué|que|how(?:'s|\s+is)|what(?:'s|\s+is))\s+(?:el\s+)?(?:clima|tiempo|weather)"
r".*?(?:en|in|for|at)\s+(.+?)(?:\?|[.!]|$)",
re.I,
),
)
_TRAILING_NOISE = re.compile(
r"\b(hoy|today|ahora|now|actual(?:mente)?|currently|por favor|please)\b.*",
re.I,
)
_CITY_ALIASES = {
"tokio": "Tokyo",
"tokyo": "Tokyo",
"cdmx": "Ciudad de México",
"mexico df": "Ciudad de México",
"df": "Ciudad de México",
"nueva york": "New York",
"new york city": "New York",
"ny": "New York",
"la": "Los Angeles",
"bs as": "Buenos Aires",
"buenos aires": "Buenos Aires",
}
def _clean_location(raw: str) -> str:
loc = _TRAILING_NOISE.sub("", (raw or "").strip()).strip(" ,.:;!?-")
if not loc:
return ""
key = loc.lower()
return _CITY_ALIASES.get(key, loc)
def parse_weather_request(text: str) -> tuple[bool, str]:
"""(is_weather, location). location vacío = usar geolocalización del usuario."""
if not text or not _WEATHER_WORDS.search(text):
return False, ""
for pat in _LOC_PATTERNS:
m = pat.search(text)
if m:
loc = _clean_location(m.group(1))
if loc:
return True, loc
return True, ""
def _desc(code: int, lang: str) -> tuple[str, str]:
icon, labels = _WMO.get(code, ("cloudy", {"es": "Nublado", "en": "Cloudy"}))
return labels.get(lang[:2].lower(), labels["en"]), icon
def _geocode(name: str) -> dict | None:
r = http_requests.get(
GEOCODE_URL,
params={"name": name, "count": 1, "language": "es", "format": "json"},
timeout=TIMEOUT,
)
r.raise_for_status()
results = r.json().get("results") or []
return results[0] if results else None
def get_weather(
location: str = "",
lang: str = "es",
lat: float | None = None,
lon: float | None = None,
) -> dict:
"""Devuelve {ok, text, widget?}."""
lang_k = lang[:2].lower() if lang else "es"
place_name = ""
loc = (location or "").strip()
if loc:
geo = _geocode(loc)
if not geo:
return {
"ok": False,
"text": f"No encontré la ubicación «{loc}». Prueba con otra ciudad.",
}
lat = geo["latitude"]
lon = geo["longitude"]
place_name = geo.get("name", loc)
if geo.get("admin1"):
place_name += f", {geo['admin1']}"
if geo.get("country"):
place_name += f", {geo['country']}"
elif lat is not None and lon is not None:
place_name = "Tu ubicación" if lang_k == "es" else "Your location"
else:
return {
"ok": False,
"text": (
"No tengo tu ubicación. Dime una ciudad, ej. «clima en Madrid», "
"o activa la geolocalización en el navegador."
),
}
r = http_requests.get(
FORECAST_URL,
params={
"latitude": lat,
"longitude": lon,
"current": "temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m",
"daily": "weather_code,temperature_2m_max,temperature_2m_min",
"timezone": "auto",
"forecast_days": 1,
},
timeout=TIMEOUT,
)
r.raise_for_status()
data = r.json()
cur = data.get("current") or {}
daily = data.get("daily") or {}
code = int(cur.get("weather_code", 3))
description, icon = _desc(code, lang_k)
temp = cur.get("temperature_2m")
humidity = cur.get("relative_humidity_2m")
wind = cur.get("wind_speed_10m")
tz = data.get("timezone", "UTC")
try:
today = datetime.now(ZoneInfo(tz)).strftime("%Y-%m-%d")
except Exception:
today = datetime.utcnow().strftime("%Y-%m-%d")
tmax = (daily.get("temperature_2m_max") or [None])[0]
tmin = (daily.get("temperature_2m_min") or [None])[0]
widget = {
"location": place_name,
"date": today,
"description": description,
"icon": icon,
"code": code,
"temp": round(temp) if temp is not None else None,
"tempMax": round(tmax) if tmax is not None else None,
"tempMin": round(tmin) if tmin is not None else None,
"humidity": humidity,
"wind": round(wind) if wind is not None else None,
"unit": "°C",
}
if lang_k == "es":
text = (
f"Clima en {place_name} hoy ({today}): {description}, "
f"{round(temp)}°C (mín {round(tmin)}° / máx {round(tmax)}°), "
f"humedad {humidity}%, viento {round(wind)} km/h."
)
else:
text = (
f"Weather in {place_name} today ({today}): {description}, "
f"{round(temp)}°C (low {round(tmin)}° / high {round(tmax)}°), "
f"humidity {humidity}%, wind {round(wind)} km/h."
)
return {"ok": True, "text": text, "widget": widget}
def get_weather_text_only(location: str, lang: str, lat=None, lon=None) -> str:
result = get_weather(location, lang, lat, lon)
return result["text"]

246
websearch.py Normal file
View file

@ -0,0 +1,246 @@
"""Búsqueda web para Arisu — DuckDuckGo, Wikipedia y Serper (opcional)."""
import os
import re
from datetime import datetime
from zoneinfo import ZoneInfo
import requests as http_requests
from duckduckgo_search import DDGS
UA = "ArisuAI/1.0 (Educational assistant; +local)"
MAX_RESULTS = 5
SNIPPET_LEN = 320
TIMEOUT = 12
_WIKI_LANG = {"es": "es", "en": "en", "ja": "ja", "fr": "fr", "de": "de", "pt": "pt", "ko": "ko", "zh": "zh"}
_DAYS_ES = ("lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo")
_MONTHS_ES = (
"enero", "febrero", "marzo", "abril", "mayo", "junio",
"julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre",
)
_RELATIVE_WORDS = (
"hoy", "today", "ahora", "now", "actual", "actualidad", "reciente",
"últim", "ultim", "esta semana", "this week", "este mes", "this month",
"en vivo", "breaking", "noticias de",
)
def _now() -> datetime:
tz = os.getenv("APP_TIMEZONE", "America/Mexico_City")
try:
return datetime.now(ZoneInfo(tz))
except Exception:
return datetime.now()
def current_date_context(lang: str = "es") -> str:
now = _now()
iso = now.strftime("%Y-%m-%d")
year = now.year
if lang.startswith("es"):
human = f"{_DAYS_ES[now.weekday()]}, {now.day} de {_MONTHS_ES[now.month - 1]} de {year}"
else:
human = now.strftime("%A, %B %d, %Y")
return (
f"FECHA Y HORA ACTUAL DEL SISTEMA (autoritativa — NO uses tu fecha de entrenamiento):\n"
f"- Hoy es: {human}\n"
f"- ISO: {iso}\n"
f"- Año en curso: {year}\n"
f"- «Hoy» / «today» / «actual» = {iso}, año {year}."
)
def enrich_search_query(query: str) -> str:
"""Añade fecha explícita si la consulta es relativa al presente."""
q = (query or "").strip()
if not q:
return q
now = _now()
iso = now.strftime("%Y-%m-%d")
year = str(now.year)
lower = q.lower()
if year in q:
return q
if any(w in lower for w in _RELATIVE_WORDS):
return f"{q} {iso}"
return q
def _clip(text: str, n: int = SNIPPET_LEN) -> str:
text = re.sub(r"<[^>]+>", "", text or "").strip()
if len(text) <= n:
return text
return text[:n] + "…"
def _serper_search(query: str, api_key: str) -> str:
resp = http_requests.post(
"https://google.serper.dev/search",
json={"q": query, "num": MAX_RESULTS},
headers={"X-API-KEY": api_key, "Content-Type": "application/json"},
timeout=TIMEOUT,
)
resp.raise_for_status()
data = resp.json()
organic = data.get("organic") or []
if not organic:
return f"No se encontraron resultados para: {query}"
blocks = []
for i, r in enumerate(organic[:MAX_RESULTS], 1):
title = r.get("title", "")
link = r.get("link", "")
snippet = _clip(r.get("snippet", ""))
blocks.append(f"{i}. {title}\nURL: {link}\n{snippet}")
return "\n\n".join(blocks)
def _ddg_search(query: str) -> str:
with DDGS() as ddgs:
hits = list(ddgs.text(query, max_results=MAX_RESULTS))
if not hits:
return ""
blocks = []
for i, r in enumerate(hits, 1):
title = (r.get("title") or "").strip()
href = (r.get("href") or "").strip()
body = _clip(r.get("body") or "")
blocks.append(f"{i}. {title}\nURL: {href}\n{body}")
return "\n\n".join(blocks)
def _wiki_search(query: str, lang: str = "es") -> str:
wiki_lang = _WIKI_LANG.get(lang[:2].lower(), "en")
headers = {"User-Agent": UA}
base = f"https://{wiki_lang}.wikipedia.org/w/api.php"
sr = http_requests.get(
base,
params={
"action": "query",
"list": "search",
"srsearch": query,
"format": "json",
"srlimit": 3,
},
headers=headers,
timeout=TIMEOUT,
)
sr.raise_for_status()
hits = sr.json().get("query", {}).get("search") or []
if not hits:
return ""
blocks = []
for i, h in enumerate(hits, 1):
title = h.get("title", "")
snippet = _clip(h.get("snippet", ""))
page_url = f"https://{wiki_lang}.wikipedia.org/wiki/{title.replace(' ', '_')}"
extract = snippet
try:
er = http_requests.get(
base,
params={
"action": "query",
"prop": "extracts",
"exintro": True,
"explaintext": True,
"titles": title,
"format": "json",
},
headers=headers,
timeout=TIMEOUT,
)
er.raise_for_status()
pages = er.json().get("query", {}).get("pages") or {}
for p in pages.values():
if p.get("extract"):
extract = _clip(p["extract"], 400)
break
except Exception:
pass
blocks.append(f"{i}. {title} (Wikipedia)\nURL: {page_url}\n{extract}")
header = f"[Wikipedia — {wiki_lang}]\n"
return header + "\n\n".join(blocks)
def web_search(query: str, lang: str = "es") -> str:
raw_q = (query or "").strip()
if not raw_q:
return "Error: consulta de búsqueda vacía"
q = enrich_search_query(raw_q)
now = _now()
date_header = f"[Búsqueda — fecha del sistema: {now.strftime('%Y-%m-%d')}]\n"
errors: list[str] = []
serper_key = os.getenv("SERPER_API_KEY", "").strip()
if serper_key:
try:
result = _serper_search(q, serper_key)
if result and "No se encontraron" not in result:
note = f"Consulta enviada: {q}\n\n" if q != raw_q else ""
return date_header + note + f"[Google/Serper]\n{result}"
except Exception as e:
errors.append(f"Serper: {e}")
try:
ddg = _ddg_search(q)
if ddg:
note = f"Consulta enviada: {q}\n\n" if q != raw_q else ""
return date_header + note + f"[DuckDuckGo]\n{ddg}"
except Exception as e:
errors.append(f"DuckDuckGo: {e}")
try:
wiki = _wiki_search(q, lang)
if wiki:
note = f"Consulta enviada: {q}\n\n" if q != raw_q else ""
return date_header + note + wiki
except Exception as e:
errors.append(f"Wikipedia: {e}")
detail = "; ".join(errors) if errors else "sin resultados"
return f"No se pudo obtener información web para «{raw_q}». ({detail})"
WEB_SEARCH_TOOL = {
"type": "function",
"function": {
"name": "web_search",
"description": (
"Busca en internet cuando no sepas algo, necesites datos actuales, "
"noticias o hechos recientes. Usa la FECHA ACTUAL del system prompt: "
"si el usuario dice «hoy» o «actual», incluye el año en curso en la query."
),
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": (
"Consulta clara. Si es sobre «hoy» o lo reciente, "
"incluye la fecha ISO o el año actual del system prompt."
),
},
},
"required": ["query"],
},
},
}
WEB_PROMPT = """
BÚSQUEDA WEB:
Si no estás segura o piden algo actual/reciente, usa web_search ANTES de inventar.
IMPORTANTE: la FECHA ACTUAL del system prompt es la verdad — NO uses 2024 ni tu corte de entrenamiento.
Si dicen «hoy», «actual», «reciente», la búsqueda debe usar el año y fecha del system prompt.
Tras buscar, resume en 2-3 oraciones con personalidad kawaii.
"""

208
workspace.py Normal file
View file

@ -0,0 +1,208 @@
"""Operaciones de workspace restringidas al directorio base del usuario."""
import os
import re
import subprocess
from pathlib import Path
MAX_FILE_BYTES = 512_000
MAX_CMD_OUTPUT = 8_000
CMD_TIMEOUT = 45
_BLOCKED_CMD = re.compile(
r"(?i)"
r"(\bsudo\b|\bsu\b|\bchmod\b|\bchown\b|\bmkfs\b|\bdd\s+if=|\bshutdown\b|\breboot\b|"
r">\s*/dev/|/\.\./|\|\s*bash|\|\s*sh\b|curl\s+.*\|\s*(ba)?sh|wget\s+.*\|\s*(ba)?sh|"
r"rm\s+-rf\s+/|rm\s+-rf\s+~|:\(\)\s*\{|>\s*/etc/)"
)
def normalize_workspace(path: str) -> str:
p = Path(path).expanduser().resolve()
if not p.is_dir():
raise ValueError(f"No es un directorio válido: {path}")
return str(p)
def resolve_path(workspace: str, rel: str = ".") -> Path:
base = Path(workspace).resolve()
target = (base / rel).resolve()
if not str(target).startswith(str(base)):
raise ValueError("Ruta fuera del directorio base")
return target
def _clip(text: str, limit: int = MAX_CMD_OUTPUT) -> str:
if len(text) <= limit:
return text
return text[:limit] + f"\n… ({len(text) - limit} caracteres omitidos)"
def tool_list_directory(workspace: str, path: str = ".") -> str:
target = resolve_path(workspace, path or ".")
if not target.exists():
return f"Error: no existe '{path}'"
if not target.is_dir():
return f"Error: '{path}' no es un directorio"
entries = sorted(target.iterdir(), key=lambda p: (not p.is_dir(), p.name.lower()))
lines = []
for e in entries[:200]:
kind = "dir" if e.is_dir() else "file"
size = e.stat().st_size if e.is_file() else 0
lines.append(f"{kind}\t{e.name}\t{size}")
if len(entries) > 200:
lines.append(f"… y {len(entries) - 200} entradas más")
return "\n".join(lines) or "(vacío)"
def tool_read_file(workspace: str, path: str) -> str:
target = resolve_path(workspace, path)
if not target.is_file():
return f"Error: '{path}' no es un archivo"
if target.stat().st_size > MAX_FILE_BYTES:
return f"Error: archivo demasiado grande (>{MAX_FILE_BYTES} bytes)"
try:
return target.read_text(encoding="utf-8")
except UnicodeDecodeError:
return "Error: no es texto UTF-8"
def tool_write_file(workspace: str, path: str, content: str) -> str:
target = resolve_path(workspace, path)
target.parent.mkdir(parents=True, exist_ok=True)
if len(content.encode("utf-8")) > MAX_FILE_BYTES:
return f"Error: contenido demasiado grande (>{MAX_FILE_BYTES} bytes)"
target.write_text(content, encoding="utf-8")
return f"OK: escrito {path} ({len(content)} caracteres)"
def tool_create_directory(workspace: str, path: str) -> str:
target = resolve_path(workspace, path)
target.mkdir(parents=True, exist_ok=True)
return f"OK: directorio creado {path}"
def tool_run_command(workspace: str, command: str) -> str:
cmd = (command or "").strip()
if not cmd:
return "Error: comando vacío"
if _BLOCKED_CMD.search(cmd):
return "Error: comando bloqueado por seguridad"
try:
proc = subprocess.run(
cmd,
shell=True,
cwd=workspace,
capture_output=True,
text=True,
timeout=CMD_TIMEOUT,
env={**os.environ, "LANG": "C.UTF-8"},
)
except subprocess.TimeoutExpired:
return f"Error: timeout ({CMD_TIMEOUT}s)"
out = (proc.stdout or "") + (proc.stderr or "")
status = f"exit_code={proc.returncode}"
if not out.strip():
return status
return status + "\n" + _clip(out)
def execute_tool(name: str, args: dict, workspace: str) -> str:
try:
if name == "list_directory":
return tool_list_directory(workspace, args.get("path", "."))
if name == "read_file":
return tool_read_file(workspace, args.get("path", ""))
if name == "write_file":
return tool_write_file(workspace, args.get("path", ""), args.get("content", ""))
if name == "create_directory":
return tool_create_directory(workspace, args.get("path", ""))
if name == "run_command":
return tool_run_command(workspace, args.get("command", ""))
return f"Error: herramienta desconocida '{name}'"
except Exception as e:
return f"Error: {e}"
TERMINAL_TOOLS = [
{
"type": "function",
"function": {
"name": "list_directory",
"description": "Lista archivos y carpetas en una ruta relativa al workspace.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Ruta relativa, por defecto '.'"},
},
},
},
},
{
"type": "function",
"function": {
"name": "read_file",
"description": "Lee un archivo de texto del workspace.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Ruta relativa al archivo"},
},
"required": ["path"],
},
},
},
{
"type": "function",
"function": {
"name": "write_file",
"description": "Crea o sobrescribe un archivo de texto en el workspace.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Ruta relativa del archivo"},
"content": {"type": "string", "description": "Contenido completo del archivo"},
},
"required": ["path", "content"],
},
},
},
{
"type": "function",
"function": {
"name": "create_directory",
"description": "Crea un directorio (y padres si hace falta) en el workspace.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Ruta relativa del directorio"},
},
"required": ["path"],
},
},
},
{
"type": "function",
"function": {
"name": "run_command",
"description": "Ejecuta un comando shell en el workspace (python, npm, git, ls, etc.).",
"parameters": {
"type": "object",
"properties": {
"command": {"type": "string", "description": "Comando shell a ejecutar"},
},
"required": ["command"],
},
},
},
]
TERMINAL_PROMPT = """
MODO TERMINAL ACTIVO — Workspace: {workspace}
Puedes usar herramientas para listar, leer, escribir archivos, crear carpetas y ejecutar comandos.
SOLO opera dentro del workspace. Usa rutas relativas.
Para crear programas: escribe archivos con write_file y prueba con run_command (python, node, etc.).
Tras usar herramientas, resume al usuario qué hiciste en 2-3 oraciones con personalidad kawaii.
Sigue incluyendo etiquetas ElevenLabs en tu respuesta final al usuario.
"""