garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /tests/e2e/relay.py

raw

#!/usr/bin/env python3
"""The toy bot's stdio relay: stdin to its Unix socket, the socket to stdout —
what a real bot ships as its own `mcp` subcommand.

    python3 relay.py SOCKET

Fails at once, on stderr, when nothing listens yet: that is what `midas node
bot start` sees while the bot is still building.
"""
import os, socket, sys, threading

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
    s.connect(sys.argv[1])
except OSError as e:
    print(f"relay: cannot connect to {sys.argv[1]}: {e.strerror}", file=sys.stderr)
    sys.exit(1)

def up():
    while True:
        data = os.read(0, 65536)
        if not data:
            break
        s.sendall(data)
    s.shutdown(socket.SHUT_WR)

threading.Thread(target=up, daemon=True).start()
while True:
    data = s.recv(65536)
    if not data:
        break
    os.write(1, data)