#!/bin/bash # ============================================================================= # opencode-attach.sh — Attach TUI to the systemd OpenCode server # ============================================================================= # # PURPOSE: # Connects the OpenCode TUI to the running systemd server (port 4096). # This ensures sessions are shared between the TUI and Telegram bot. # # USAGE: # opencode-attach — Attach to systemd server # opencode-attach --omoa — Start systemd server with omoa, then attach # # WHY THIS EXISTS: # - The systemd server runs on port 4096 (managed by Telegram or systemctl) # - The TUI can either start its own server (separate sessions) or attach # - Attaching shares sessions between TUI and Telegram bot # - omoa is managed via systemd/Telegram, not from the terminal # # UPSTREAM REPO: # https://git.hibbhome.com/Hibbhome/opencode-omoa-toggle # # CREATED: 2026-05-03 # ============================================================================= OPENCODE_BIN="$HOME/.opencode/bin/opencode" SERVER_URL="http://localhost:4096" # If --omoa flag, ensure systemd server has omoa enabled if [[ "$*" == *"--omoa"* ]]; then touch "$HOME/.config/opencode/omoa-enabled" systemctl --user restart opencode-serve.service sleep 2 fi # Check if server is running if ! curl -s "$SERVER_URL" > /dev/null 2>&1; then echo "Starting OpenCode server..." systemctl --user start opencode-serve.service sleep 2 fi echo "Attaching to OpenCode server at $SERVER_URL..." exec "$OPENCODE_BIN" attach "$SERVER_URL"