|
#!/usr/bin/env bash |
|
# optimize-omo.sh — Optimize oh-my-openagent for efficiency |
|
# Disables telemetry, reduces noise, keeps diagnostics. |
|
# |
|
# One-liner install: |
|
# curl -fsSL https://gist.githubusercontent.com/RAW_URL | bash |
|
# wget -qO- https://gist.githubusercontent.com/RAW_URL | bash |
|
|
|
set -euo pipefail |
|
|
|
# --- Helpers --- |
|
step() { printf ' \033[36m[*]\033[0m %s\n' "$1"; } |
|
ok() { printf ' \033[32m[+]\033[0m %s\n' "$1"; } |
|
warn() { printf ' \033[33m[!]\033[0m %s\n' "$1"; } |
|
err() { printf ' \033[31m[-]\033[0m %s\n' "$1"; } |
|
|
|
echo "" |
|
printf ' \033[35moh-my-openagent efficiency optimizer\033[0m\n' |
|
printf ' \033[90m=====================================\033[0m\n' |
|
echo "" |
|
|
|
# --- Detect OS --- |
|
OS="$(uname -s)" |
|
case "$OS" in |
|
Darwin) PLATFORM="macos" ;; |
|
Linux) PLATFORM="linux" ;; |
|
*) err "Unsupported OS: $OS"; exit 1 ;; |
|
esac |
|
|
|
# --- Locate config --- |
|
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/opencode" |
|
CONFIG_FILE="$CONFIG_DIR/oh-my-opencode.json" |
|
CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/opencode" |
|
|
|
if [ ! -d "$CONFIG_DIR" ]; then |
|
err "OpenCode config directory not found: $CONFIG_DIR" |
|
err "Is opencode installed? Run 'opencode' first to initialize." |
|
exit 1 |
|
fi |
|
|
|
# --- 1. Environment variables --- |
|
step "Setting telemetry opt-out environment variables..." |
|
|
|
# Determine shell profile |
|
SHELL_NAME="$(basename "${SHELL:-/bin/bash}")" |
|
case "$SHELL_NAME" in |
|
zsh) PROFILE="$HOME/.zshrc" ;; |
|
bash) |
|
if [ "$PLATFORM" = "macos" ]; then |
|
PROFILE="$HOME/.bash_profile" |
|
else |
|
PROFILE="$HOME/.bashrc" |
|
fi |
|
;; |
|
fish) PROFILE="$HOME/.config/fish/config.fish" ;; |
|
*) PROFILE="$HOME/.profile" ;; |
|
esac |
|
|
|
set_env_var() { |
|
local key="$1" val="$2" |
|
# Set for current session |
|
export "$key=$val" |
|
|
|
# Check if already in profile |
|
if [ -f "$PROFILE" ] && grep -q "^export ${key}=" "$PROFILE" 2>/dev/null; then |
|
# Update existing |
|
if [ "$PLATFORM" = "macos" ]; then |
|
sed -i '' "s|^export ${key}=.*|export ${key}=\"${val}\"|" "$PROFILE" |
|
else |
|
sed -i "s|^export ${key}=.*|export ${key}=\"${val}\"|" "$PROFILE" |
|
fi |
|
ok "$key already in $PROFILE (updated)" |
|
elif [ -f "$PROFILE" ] && [ "$SHELL_NAME" = "fish" ] && grep -q "^set -gx ${key}" "$PROFILE" 2>/dev/null; then |
|
if [ "$PLATFORM" = "macos" ]; then |
|
sed -i '' "s|^set -gx ${key}.*|set -gx ${key} \"${val}\"|" "$PROFILE" |
|
else |
|
sed -i "s|^set -gx ${key}.*|set -gx ${key} \"${val}\"|" "$PROFILE" |
|
fi |
|
ok "$key already in $PROFILE (updated)" |
|
else |
|
# Append |
|
if [ "$SHELL_NAME" = "fish" ]; then |
|
echo "set -gx ${key} \"${val}\"" >> "$PROFILE" |
|
else |
|
echo "export ${key}=\"${val}\"" >> "$PROFILE" |
|
fi |
|
ok "$key = $val (added to $PROFILE)" |
|
fi |
|
} |
|
|
|
set_env_var "OMO_SEND_ANONYMOUS_TELEMETRY" "0" |
|
set_env_var "OMO_DISABLE_POSTHOG" "1" |
|
|
|
# --- 2. Config file: add disabled_hooks --- |
|
step "Configuring oh-my-opencode.json..." |
|
|
|
# We need jq or python3 for JSON manipulation |
|
if command -v jq &>/dev/null; then |
|
JSON_TOOL="jq" |
|
elif command -v python3 &>/dev/null; then |
|
JSON_TOOL="python3" |
|
else |
|
err "Neither jq nor python3 found. Install one to continue." |
|
exit 1 |
|
fi |
|
|
|
HOOKS_TO_DISABLE='["auto-update-checker","startup-toast","agent-usage-reminder"]' |
|
|
|
if [ -f "$CONFIG_FILE" ]; then |
|
if [ "$JSON_TOOL" = "jq" ]; then |
|
# Merge disabled_hooks without duplicates |
|
UPDATED=$(jq --argjson hooks "$HOOKS_TO_DISABLE" ' |
|
.disabled_hooks = ((.disabled_hooks // []) + $hooks | unique) |
|
' "$CONFIG_FILE") |
|
echo "$UPDATED" > "$CONFIG_FILE" |
|
else |
|
python3 -c " |
|
import json, sys |
|
|
|
hooks_to_add = $HOOKS_TO_DISABLE |
|
|
|
with open('$CONFIG_FILE', 'r') as f: |
|
config = json.load(f) |
|
|
|
existing = config.get('disabled_hooks', []) |
|
merged = list(dict.fromkeys(existing + hooks_to_add)) # unique, order preserved |
|
config['disabled_hooks'] = merged |
|
|
|
with open('$CONFIG_FILE', 'w') as f: |
|
json.dump(config, f, indent=2) |
|
f.write('\n') |
|
" |
|
fi |
|
ok "disabled_hooks configured in $CONFIG_FILE" |
|
else |
|
# Create minimal config |
|
if [ "$JSON_TOOL" = "jq" ]; then |
|
jq -n --argjson hooks "$HOOKS_TO_DISABLE" '{ |
|
"$schema": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json", |
|
"disabled_hooks": $hooks, |
|
"agents": {}, |
|
"categories": {} |
|
}' > "$CONFIG_FILE" |
|
else |
|
python3 -c " |
|
import json |
|
config = { |
|
'\$schema': 'https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json', |
|
'disabled_hooks': $HOOKS_TO_DISABLE, |
|
'agents': {}, |
|
'categories': {} |
|
} |
|
with open('$CONFIG_FILE', 'w') as f: |
|
json.dump(config, f, indent=2) |
|
f.write('\n') |
|
" |
|
fi |
|
ok "Created $CONFIG_FILE with disabled hooks" |
|
fi |
|
|
|
# --- 3. Clean stale tmp files --- |
|
step "Cleaning stale files..." |
|
for tmp in "$CONFIG_DIR/oh-my-openagent.json.tmp" "$CONFIG_DIR/oh-my-opencode.json.tmp"; do |
|
if [ -f "$tmp" ]; then |
|
rm -f "$tmp" |
|
ok "Removed $(basename "$tmp")" |
|
fi |
|
done |
|
|
|
# --- 4. Update runtime cache --- |
|
step "Updating oh-my-opencode in runtime cache..." |
|
if [ -d "$CACHE_DIR" ]; then |
|
if command -v bun &>/dev/null; then |
|
(cd "$CACHE_DIR" && bun add oh-my-opencode@latest 2>/dev/null) && ok "Updated via bun" || warn "bun update failed (non-critical)" |
|
elif command -v npm &>/dev/null; then |
|
(cd "$CACHE_DIR" && npm install oh-my-opencode@latest 2>/dev/null) && ok "Updated via npm" || warn "npm update failed (non-critical)" |
|
else |
|
warn "Neither bun nor npm found — will update on next opencode launch" |
|
fi |
|
else |
|
warn "Cache directory not found: $CACHE_DIR (will update on next opencode launch)" |
|
fi |
|
|
|
# --- 5. Doctor check --- |
|
step "Running health check..." |
|
if command -v npx &>/dev/null; then |
|
(cd "$CONFIG_DIR" && npx oh-my-openagent@latest doctor 2>&1) | while IFS= read -r line; do |
|
echo " $line" |
|
done |
|
else |
|
warn "npx not found — skipping doctor" |
|
fi |
|
|
|
# --- Summary --- |
|
echo "" |
|
printf ' \033[32mDone! Changes take effect on next opencode launch.\033[0m\n' |
|
printf ' \033[90mReload your shell or run: source %s\033[0m\n' "$PROFILE" |
|
echo "" |