File size: 738 Bytes
38b4eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# terminal_ws.py
#
# Terminal WebSocket Endpoint
# - Accepts commands from frontend
# - Streams output back

from fastapi import APIRouter, WebSocket
from neuralai_engine import run_shell_command

router = APIRouter()


@router.websocket("/ws/terminal")
async def terminal_ws(ws: WebSocket):
    """
    WebSocket endpoint for terminal.
    Frontend connects, sends command, receives streamed output.
    """
    await ws.accept()
    
    try:
        cmd = await ws.receive_text()
        
        async for line in run_shell_command(cmd):
            await ws.send_text(line)
        
        await ws.send_text("[DONE]")
    except Exception as e:
        await ws.send_text(f"[Error] {str(e)}")
    finally:
        await ws.close()