opencode-omoa-toggle/opencode-start.sh

64 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# opencode-start.sh — Start OpenCode with or without oh-my-openagent
# =============================================================================
#
# PURPOSE:
# Wrapper script that starts the OpenCode server, optionally injecting the
# oh-my-openagent plugin at runtime via OPENCODE_CONFIG_CONTENT.
# No files are mutated — the base opencode.json stays clean.
#
# USAGE:
# ./opencode-start.sh — Start without omoa (vanilla)
# ./opencode-start.sh --omoa — Start with omoa enabled
#
# HOW IT WORKS:
# - Reads ~/.config/opencode/opencode.json
# - If --omoa flag is set, uses python3 to add oh-my-openagent@latest to the
# plugin array and sets OPENCODE_CONFIG_CONTENT with the modified JSON
# - If --omoa is not set, starts with the clean config file
# - OPENCODE_CONFIG_CONTENT has higher precedence than the config file,
# so the plugin is only active for that process
#
# 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"
PORT=4096
if [[ "$*" == *"--omoa"* ]]; 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