From a998c3975cd91262e120cbf2259c106e5b26733a Mon Sep 17 00:00:00 2001 From: Twentyninehairs_bot Date: Sun, 3 May 2026 20:23:28 -0700 Subject: [PATCH] Add wrapper script for runtime omoa injection via OPENCODE_CONFIG_CONTENT --- README.md | 52 ++++++++++++++++++++++---------------- omoa-restart.mjs | 60 ++++++++++++++++++++++++++++++++++++++++++++ opencode-start.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 21 deletions(-) create mode 100644 omoa-restart.mjs create mode 100755 opencode-start.sh diff --git a/README.md b/README.md index c672c4f..b064bec 100644 --- a/README.md +++ b/README.md @@ -4,45 +4,55 @@ Toggle the [oh-my-openagent](https://github.com/code-yeongyu/oh-my-openagent) pl ## Purpose -oh-my-openagent adds multi-model orchestration, LSP tools, and other advanced features to OpenCode. However, it can interfere with normal operation. This tool provides a simple way to enable/disable it. +oh-my-openagent adds multi-model orchestration, LSP tools, and other advanced features to OpenCode. However, it can interfere with normal operation. This tool provides a simple way to enable/disable it **without modifying the config file**. + +## How It Works + +The solution uses the `OPENCODE_CONFIG_CONTENT` environment variable, which has higher runtime precedence than the config file. This means: + +- The base `opencode.json` stays clean (no plugin entry) +- The plugin is only injected at runtime when needed +- No file mutation, no backups, no restore ## Files -- `omoa.sh` — Shell script for direct use -- `omoa.mjs` — OpenCode skill for integration with OpenCode sessions and Telegram bot +- `opencode-start.sh` — Wrapper script that starts OpenCode with/without omoa +- `omoa-restart.mjs` — OpenCode skill for Telegram bot integration +- `README.md` — This file ## Usage ### Shell Script ```bash -./omoa.sh # Show status -./omoa.sh on # Enable oh-my-openagent -./omoa.sh off # Disable oh-my-openagent +./opencode-start.sh # Start without omoa (vanilla) +./opencode-start.sh --omoa # Start with omoa enabled ``` -### OpenCode Skill +### Telegram Bot -When registered in `opencode.json`, use `/commands` → `omoa_toggle` in OpenCode or Telegram. +When registered in `opencode.json` under "skills", use `/commands` in Telegram: +- `restart_with_omoa` — Restart OpenCode with omoa enabled +- `restart_without_omoa` — Restart OpenCode without omoa (vanilla) ## Installation -1. Copy `omoa.sh` and `omoa.mjs` to `/home/kenny/.config/opencode/skills/` -2. Add to `opencode.json` under "skills": +1. Copy files to `~/.config/opencode/` +2. Update systemd service to use `opencode-start.sh`: + ```ini + [Service] + ExecStart=/home/kenny/.config/opencode/opencode-start.sh + ``` +3. Add skill to `opencode.json`: ```json - "omoa": { - "description": "Toggle oh-my-openagent plugin on/off", - "path": "/home/kenny/.config/opencode/skills/omoa.mjs" + "skills": { + "omoa-restart": { + "description": "Restart OpenCode with/without oh-my-openagent", + "path": "/home/kenny/.config/opencode/skills/omoa-restart.mjs" + } } ``` -3. Restart OpenCode server - -## Known Issues - -- The omoa install command is fragile and may fail silently -- The `--no-tui` flag requires all options to be specified manually -- The script does not verify success before restarting services -- Needs overhaul to handle edge cases better +4. Restart services ## Author diff --git a/omoa-restart.mjs b/omoa-restart.mjs new file mode 100644 index 0000000..433095d --- /dev/null +++ b/omoa-restart.mjs @@ -0,0 +1,60 @@ +/** + * omoa-restart.mjs — OpenCode skills to restart server with/without oh-my-openagent + * + * PURPOSE: + * Provides two skills for the Telegram bot: + * - restart_with_omoa: Restart OpenCode with oh-my-openagent enabled + * - restart_without_omoa: Restart OpenCode without oh-my-openagent (vanilla) + * + * HOW IT WORKS: + * - Uses systemctl to restart the opencode-serve service + * - The service uses opencode-start.sh which conditionally sets + * OPENCODE_CONFIG_CONTENT based on the --omoa flag + * - No files are mutated — the base opencode.json stays clean + * + * UPSTREAM REPO: + * https://git.hibbhome.com/Hibbhome/opencode-omoa-toggle + * + * CREATED: 2026-05-03 + */ + +import { tool } from "@opencode-ai/plugin/tool"; +import { execSync } from "node:child_process"; + +export const OmoaRestartPlugin = async (_ctx) => { + return { + tool: { + restart_with_omoa: tool({ + description: "Restart OpenCode server with oh-my-openagent plugin enabled", + args: {}, + async execute() { + try { + // Update the systemd service to use --omoa flag + execSync(`sed -i 's|opencode-start.sh"|opencode-start.sh --omoa"|' /home/kenny/.config/systemd/user/opencode-serve.service`); + execSync("systemctl --user daemon-reload"); + execSync("systemctl --user restart opencode-serve.service"); + return "OpenCode restarted WITH oh-my-openagent enabled"; + } catch (err) { + return `Error: ${err.message}`; + } + }, + }), + + restart_without_omoa: tool({ + description: "Restart OpenCode server without oh-my-openagent (vanilla)", + args: {}, + async execute() { + try { + // Update the systemd service to remove --omoa flag + execSync(`sed -i 's|opencode-start.sh --omoa"|opencode-start.sh"|' /home/kenny/.config/systemd/user/opencode-serve.service`); + execSync("systemctl --user daemon-reload"); + execSync("systemctl --user restart opencode-serve.service"); + return "OpenCode restarted WITHOUT oh-my-openagent (vanilla)"; + } catch (err) { + return `Error: ${err.message}`; + } + }, + }), + }, + }; +}; diff --git a/opencode-start.sh b/opencode-start.sh new file mode 100755 index 0000000..d207d4e --- /dev/null +++ b/opencode-start.sh @@ -0,0 +1,63 @@ +#!/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