#!/bin/bash # ============================================================================= # opencode-start.sh — Start OpenCode with or without oh-my-openagent # ============================================================================= # # PURPOSE: # Wrapper script that starts the OpenCode server. If a state file exists # at ~/.config/opencode/omoa-enabled, the oh-my-openagent plugin is injected # at runtime via OPENCODE_CONFIG_CONTENT. Otherwise, OpenCode starts vanilla. # # HOW IT WORKS: # - Checks for state file: ~/.config/opencode/omoa-enabled # - If state file exists: # - Reads opencode.json # - Uses python3 to add oh-my-openagent@latest to the plugin array # - Sets OPENCODE_CONFIG_CONTENT with the modified JSON # - OPENCODE_CONFIG_CONTENT has higher precedence than the config file # - If state file does not exist: # - Starts OpenCode with the clean config file (no omoa) # - No files are mutated — the base opencode.json stays clean # # STATE FILE: # ~/.config/opencode/omoa-enabled # - If this file exists, omoa is enabled # - If this file does not exist, omoa is disabled # - Created/deleted by the omoa_toggle Telegram skill # # USAGE: # opencode-start — Start without omoa (vanilla) # opencode-start --omoa — Start with omoa enabled # # SYSTEMD INTEGRATION: # This script is used as the ExecStart command in the opencode-serve.service. # The systemd service runs this in the background. # # UPSTREAM REPO: # https://git.hibbhome.com/Hibbhome/opencode-omoa-toggle # # CREATED: 2026-05-03 # ============================================================================= CONFIG_FILE="$HOME/.config/opencode/opencode.json" OPENCODE_BIN="$HOME/.opencode/bin/opencode" STATE_FILE="$HOME/.config/opencode/omoa-enabled" PORT=4096 # Check if omoa should be enabled # Either via state file or --omoa flag ENABLE_OMOA=false if [ -f "$STATE_FILE" ]; then ENABLE_OMOA=true fi if [[ "$*" == *"--omoa"* ]]; then ENABLE_OMOA=true fi if [ "$ENABLE_OMOA" = true ]; then # Inject oh-my-openagent plugin at runtime using python3 MODIFIED_CONFIG=$(python3 -c " import json import sys with open('$CONFIG_FILE', 'r') as f: config = json.load(f) # Ensure plugin array exists if 'plugin' not in config: config['plugin'] = [] # Add oh-my-openagent if not already present if not any('oh-my-openagent' in p for p in config['plugin']): config['plugin'].append('oh-my-openagent@latest') print(json.dumps(config)) ") if [ $? -ne 0 ]; then echo "ERROR: Failed to process config with python3" exit 1 fi echo "Starting OpenCode WITH oh-my-openagent..." OPENCODE_CONFIG_CONTENT="$MODIFIED_CONFIG" exec "$OPENCODE_BIN" serve --port "$PORT" else echo "Starting OpenCode (vanilla)..." exec "$OPENCODE_BIN" serve --port "$PORT" fi