feat: JARVIS v3 Phase 1 — WebSocket relay vers Hermes A.L.E.X.A.
- Serveur WebSocket direct avec websockets 16.0 - Interface HTML/CSS/JS dark theme avec chat - Relay vers API Hermes sur port 8642 - Historique de conversation (20 messages) - Clé API via Infisical - Testé et fonctionnel sur openclaw1.dev.home:7081
This commit is contained in:
+205
@@ -0,0 +1,205 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
JARVIS v3 Phase 1 — Relay WebSocket ↔ Hermes API
|
||||
==================================================
|
||||
PoC minimal : WebSocket texte → API Hermes → réponse texte.
|
||||
Pas de Pipecat — intégration directe pour valider le concept.
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import httpx
|
||||
from websockets.asyncio.server import serve
|
||||
from websockets.http11 import Response, Headers
|
||||
|
||||
PORT = int(os.environ.get("JARVIS_PORT", "7081"))
|
||||
|
||||
# ─── Clé API ─────────────────────────────────────────────────
|
||||
def get_hermes_key():
|
||||
key = os.environ.get("HERMES_API_KEY", "") or os.environ.get("HERMES_ALEXA_KEY", "")
|
||||
if key: return key
|
||||
try:
|
||||
creds = json.load(open("/opt/infisical/admin-credentials.json"))
|
||||
r = subprocess.run(
|
||||
["/home/openclaw/.local/bin/infisical", "secrets", "get",
|
||||
"API_SERVER_KEY", "--domain", "https://secret.dracodev.net",
|
||||
"--projectId", "192c1eed-6666-41ee-80df-a4636a2da0ad",
|
||||
"--env", "prod", "--path", "/hermes-claw", "--plain",
|
||||
"--token", creds["token"]],
|
||||
capture_output=True, text=True, timeout=15)
|
||||
return r.stdout.strip()
|
||||
except Exception as e:
|
||||
print(f"⚠️ Infisical: {e}"); return ""
|
||||
|
||||
HERMES_KEY = get_hermes_key()
|
||||
HERMES_URL = "http://localhost:8642/v1/chat/completions"
|
||||
|
||||
SYSTEM_PROMPT = (
|
||||
"Tu es JARVIS, l'assistant vocal de Bruno. "
|
||||
"Parle en français, sois concis (3 phrases max), chaleureux et utile. "
|
||||
"Pas de markdown. Réponds comme si tu parlais à l'oral."
|
||||
)
|
||||
|
||||
# ─── Page HTML ───────────────────────────────────────────────
|
||||
HTML_PAGE = """<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>JARVIS v3 — Phase 1 PoC</title>
|
||||
<style>
|
||||
:root{--bg:#010314;--surface:#060e24;--text:#cdd6f4;--cyan:#06d6f0;--blue:#3b82f6;--green:#10b981;--red:#ef4444;--border:#121e3d}
|
||||
*{box-sizing:border-box;margin:0;padding:0}
|
||||
body{font-family:system-ui,sans-serif;background:var(--bg);color:var(--text);
|
||||
height:100vh;display:flex;flex-direction:column;align-items:center;padding:16px}
|
||||
h1{color:var(--cyan);font-size:1.2em;margin-bottom:6px}
|
||||
.status{font-size:.8em;padding:4px 12px;border-radius:6px;margin-bottom:8px}
|
||||
.on{background:#10b98122;color:var(--green);border:1px solid var(--green)}
|
||||
.off{background:#ef444422;color:var(--red);border:1px solid var(--red)}
|
||||
#chat{width:100%;max-width:600px;flex:1;overflow-y:auto;
|
||||
background:var(--surface);border:1px solid var(--border);border-radius:10px;padding:12px;margin-bottom:10px}
|
||||
.msg{margin:6px 0;padding:8px 12px;border-radius:8px;max-width:85%;font-size:.95em}
|
||||
.user{background:var(--blue);margin-left:auto;text-align:right}
|
||||
.jarvis{background:#1a1a3e;margin-right:auto;border-left:2px solid var(--cyan)}
|
||||
.sys{text-align:center;color:#4a5a7f;font-style:italic;font-size:.8em}
|
||||
.row{display:flex;width:100%;max-width:600px;gap:8px}
|
||||
input{flex:1;padding:10px 12px;border-radius:8px;border:1px solid var(--border);
|
||||
background:var(--surface);color:var(--text);font-size:1em}
|
||||
button{padding:10px 20px;border-radius:8px;border:none;background:var(--cyan);
|
||||
color:var(--bg);font-weight:bold;cursor:pointer;font-size:.95em}
|
||||
button:hover{opacity:.9}
|
||||
button:disabled{opacity:.5;cursor:default}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>⚡ JARVIS v3 — Phase 1 (Hermes direct)</h1>
|
||||
<div id="status" class="off">🔴 Déconnecté</div>
|
||||
<div id="chat"></div>
|
||||
<div class="row">
|
||||
<input id="inp" placeholder="Pose ta question..." autofocus>
|
||||
<button id="btn">Envoyer</button>
|
||||
</div>
|
||||
<script>
|
||||
const s=document.getElementById('status'),c=document.getElementById('chat'),
|
||||
i=document.getElementById('inp'),b=document.getElementById('btn');
|
||||
let ws=null;
|
||||
function m(t,r){const d=document.createElement('div');d.className='msg '+r;d.textContent=t;c.appendChild(d);c.scrollTop=c.scrollHeight}
|
||||
function connect(){ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'/ws');
|
||||
ws.onopen=()=>{s.textContent='🟢 Connecté — JARVIS v3 prêt';s.className='on';b.disabled=false;i.focus()};
|
||||
ws.onclose=()=>{s.textContent='🔴 Déconnecté';s.className='off';b.disabled=true;setTimeout(connect,2000)};
|
||||
ws.onmessage=e=>{try{let d=JSON.parse(e.data);if(d.type==='welcome')m(d.content,'sys');
|
||||
else if(d.type==='response')m(d.content,'jarvis');else if(d.type==='thinking')s.textContent='🤔 JARVIS réfléchit...';
|
||||
else if(d.type==='error')m('❌ '+d.content,'sys')}catch(x){m(e.data,'jarvis')};s.textContent='🟢 Connecté — JARVIS v3 prêt';b.disabled=false}}
|
||||
function send(){let t=i.value.trim();if(!t||!ws||ws.readyState!==WebSocket.OPEN)return;m(t,'user');ws.send(JSON.stringify({text:t}));i.value='';b.disabled=true;s.textContent='🤔 JARVIS réfléchit...'}
|
||||
b.onclick=send;i.onkeydown=e=>{if(e.key==='Enter')send()};connect()
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
# ─── HTTP Handler ────────────────────────────────────────────
|
||||
async def http_handler(websocket):
|
||||
"""Serve the test page over raw HTTP (before upgrade)."""
|
||||
# This is handled by serve()'s process_request
|
||||
pass
|
||||
|
||||
async def process_request(connection, request):
|
||||
"""Serve HTML for HTTP requests, upgrade for WebSocket."""
|
||||
if request.path == "/" or request.path == "/test.html":
|
||||
headers = Headers([("Content-Type", "text/html; charset=utf-8")])
|
||||
return Response(200, "OK", headers, HTML_PAGE.encode())
|
||||
return None # Let WebSocket handler take over
|
||||
|
||||
# ─── WebSocket Handler ───────────────────────────────────────
|
||||
async def ws_handler(websocket):
|
||||
"""Handle WebSocket connection: relay messages to Hermes API."""
|
||||
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
||||
client = httpx.AsyncClient(timeout=60.0)
|
||||
|
||||
try:
|
||||
# Send welcome
|
||||
await websocket.send(json.dumps({
|
||||
"type": "welcome",
|
||||
"content": "👋 Bonjour Bruno ! Je suis JARVIS v3 (Phase 1). Pose-moi une question !"
|
||||
}))
|
||||
print("✅ Client connecté")
|
||||
|
||||
async for raw in websocket:
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
text = data.get("text", "").strip()
|
||||
except:
|
||||
text = raw.strip()
|
||||
|
||||
if not text:
|
||||
continue
|
||||
|
||||
print(f"👤 {text[:80]}")
|
||||
messages.append({"role": "user", "content": text})
|
||||
# Keep last 20 messages + system prompt
|
||||
if len(messages) > 21:
|
||||
messages = [messages[0]] + messages[-20:]
|
||||
|
||||
# Send thinking indicator
|
||||
await websocket.send(json.dumps({"type": "thinking"}))
|
||||
|
||||
# Call Hermes API
|
||||
try:
|
||||
resp = await client.post(
|
||||
HERMES_URL,
|
||||
headers={
|
||||
"Authorization": f"Bearer {HERMES_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={
|
||||
"model": "Hermes Agent",
|
||||
"messages": messages,
|
||||
"temperature": 0.8,
|
||||
"max_tokens": 300,
|
||||
"stream": False,
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
body = resp.json()
|
||||
reply = body["choices"][0]["message"]["content"]
|
||||
|
||||
messages.append({"role": "assistant", "content": reply})
|
||||
print(f"🤖 {reply[:80]}")
|
||||
|
||||
await websocket.send(json.dumps({
|
||||
"type": "response",
|
||||
"content": reply,
|
||||
}))
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ API error: {e}")
|
||||
await websocket.send(json.dumps({
|
||||
"type": "error",
|
||||
"content": f"Erreur API: {str(e)[:200]}",
|
||||
}))
|
||||
|
||||
except Exception as e:
|
||||
print(f"👋 Déconnecté: {e}")
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
|
||||
# ─── Main ────────────────────────────────────────────────────
|
||||
async def main():
|
||||
if not HERMES_KEY:
|
||||
print("❌ Pas de clé API Hermes"); sys.exit(1)
|
||||
|
||||
print(f"🔑 {HERMES_KEY[:15]}...")
|
||||
print(f"🌐 {HERMES_URL}")
|
||||
print(f"🚀 JARVIS v3 PoC → http://0.0.0.0:{PORT}")
|
||||
print(f" WebSocket → ws://0.0.0.0:{PORT}/ws")
|
||||
|
||||
async with serve(ws_handler, "0.0.0.0", PORT, process_request=process_request):
|
||||
print(f"✅ Serveur prêt sur le port {PORT}")
|
||||
await asyncio.get_running_loop().create_future() # run forever
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user