main.py aktualisiert
This commit is contained in:
37
main.py
37
main.py
@@ -256,36 +256,41 @@ async def chat_endpoint(websocket: WebSocket):
|
|||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
user_msg = await websocket.receive_text()
|
user_msg = await websocket.receive_text()
|
||||||
|
|
||||||
# 1. Dynamischen Prompt laden
|
|
||||||
sys_prompt = get_system_prompt()
|
sys_prompt = get_system_prompt()
|
||||||
|
|
||||||
# 2. KI fragen
|
|
||||||
ai_response = await get_ai_response(user_msg, sys_prompt)
|
ai_response = await get_ai_response(user_msg, sys_prompt)
|
||||||
|
|
||||||
# 3. XML-Tags auslesen (<EXECUTE target="IP">Befehl</EXECUTE>)
|
# Befehle extrahieren
|
||||||
commands_to_run = re.findall(r'<EXECUTE target="(.*?)">(.*?)</EXECUTE>', ai_response, re.IGNORECASE | re.DOTALL)
|
commands_to_run = re.findall(r'<EXECUTE target="(.*?)">(.*?)</EXECUTE>', ai_response, re.IGNORECASE | re.DOTALL)
|
||||||
|
|
||||||
# 4. XML-Tags für das UI ausblenden, damit der Chat sauber aussieht
|
|
||||||
clean_chat_msg = re.sub(r'<EXECUTE.*?>.*?</EXECUTE>', '', ai_response, flags=re.IGNORECASE | re.DOTALL).strip()
|
clean_chat_msg = re.sub(r'<EXECUTE.*?>.*?</EXECUTE>', '', ai_response, flags=re.IGNORECASE | re.DOTALL).strip()
|
||||||
|
|
||||||
if clean_chat_msg:
|
if clean_chat_msg:
|
||||||
await websocket.send_text(clean_chat_msg)
|
await websocket.send_text(clean_chat_msg)
|
||||||
|
|
||||||
# 5. Gefundene Befehle an den Pi schicken!
|
if commands_to_run:
|
||||||
|
# Liste für alle laufenden Tasks erstellen
|
||||||
|
tasks = []
|
||||||
for target_ip, cmd in commands_to_run:
|
for target_ip, cmd in commands_to_run:
|
||||||
target_ip = target_ip.strip()
|
|
||||||
cmd = cmd.strip()
|
|
||||||
|
|
||||||
conn = get_db()
|
conn = get_db()
|
||||||
node = conn.execute('SELECT * FROM nodes WHERE ip = ? OR name = ?', (target_ip, target_ip)).fetchone()
|
node = conn.execute('SELECT * FROM nodes WHERE ip = ? OR name = ?', (target_ip.strip(), target_ip.strip())).fetchone()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
if node:
|
if node:
|
||||||
# Leite Aufgabe an die System Logs weiter
|
# Wir erstellen den Task, starten ihn aber noch nicht separat
|
||||||
asyncio.create_task(run_remote_task(node['ip'], node['user'], cmd, "KI-Kommando"))
|
tasks.append(run_remote_task(node['ip'], node['user'], cmd.strip(), "KI-Kommando"))
|
||||||
else:
|
|
||||||
await websocket.send_text(f"⚠️ Konnte Node {target_ip} in DB nicht finden.")
|
if tasks:
|
||||||
|
# Dem Nutzer im Chat kurz Bescheid geben
|
||||||
|
await websocket.send_text("ℹ️ *Warte auf Rückmeldungen der Nodes...*")
|
||||||
|
|
||||||
|
# Jetzt werden alle SSH-Befehle gleichzeitig gestartet und abgewartet
|
||||||
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
|
# Sobald asyncio.gather fertig ist, geht es hier weiter mit dem Follow-up:
|
||||||
|
follow_up_prompt = "Die Befehle wurden ausgeführt. Bitte fasse die Ergebnisse kurz zusammen."
|
||||||
|
ai_summary = await get_ai_response(follow_up_prompt, sys_prompt)
|
||||||
|
|
||||||
|
await websocket.send_text("--- Ergebnis-Zusammenfassung ---")
|
||||||
|
await websocket.send_text(ai_summary)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Chat Fehler: {e}")
|
print(f"Chat Fehler: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user