|
6 | 6 |
|
7 | 7 | bashio::log.info "Initializing HA OpenCode..." |
8 | 8 |
|
| 9 | +# ============================================================================== |
| 10 | +# Read configuration options early (needed for CPU detection) |
| 11 | +# ============================================================================== |
| 12 | + |
| 13 | +CPU_MODE_CONFIG=$(bashio::config 'cpu_mode' || echo "auto") |
| 14 | +MCP_ENABLED=$(bashio::config 'mcp_enabled') |
| 15 | +LSP_ENABLED=$(bashio::config 'lsp_enabled' || echo "true") |
| 16 | + |
| 17 | +bashio::log.info "Configuration: MCP=${MCP_ENABLED} LSP=${LSP_ENABLED} CPU_MODE=${CPU_MODE_CONFIG}" |
| 18 | + |
| 19 | +# ============================================================================== |
| 20 | +# CPU Capability Detection - Select appropriate OpenCode binary |
| 21 | +# OpenCode provides baseline binaries for CPUs without AVX2 support |
| 22 | +# ============================================================================== |
| 23 | + |
| 24 | +# Run CPU detection (respects config override) |
| 25 | +detect_cpu_capabilities() { |
| 26 | + local cpu_mode_config="${CPU_MODE_CONFIG:-auto}" |
| 27 | + |
| 28 | + if [ "${cpu_mode_config}" = "baseline" ]; then |
| 29 | + echo "baseline" > /data/.cpu_mode |
| 30 | + bashio::log.info "CPU mode forced to baseline by configuration" |
| 31 | + return |
| 32 | + elif [ "${cpu_mode_config}" = "regular" ]; then |
| 33 | + echo "regular" > /data/.cpu_mode |
| 34 | + bashio::log.info "CPU mode forced to regular by configuration" |
| 35 | + return |
| 36 | + fi |
| 37 | + |
| 38 | + local cpu_flags="" |
| 39 | + local has_avx2=false |
| 40 | + local has_avx=false |
| 41 | + |
| 42 | + if [ -f /proc/cpuinfo ]; then |
| 43 | + cpu_flags=$(cat /proc/cpuinfo | grep -oP 'flags\s*:\s*\K.*' | head -1) |
| 44 | + fi |
| 45 | + |
| 46 | + if echo "${cpu_flags}" | grep -q '\bavx2\b'; then |
| 47 | + has_avx2=true |
| 48 | + fi |
| 49 | + |
| 50 | + if echo "${cpu_flags}" | grep -q '\bavx\b'; then |
| 51 | + has_avx=true |
| 52 | + fi |
| 53 | + |
| 54 | + if [ "${has_avx2}" = "true" ]; then |
| 55 | + echo "regular" > /data/.cpu_mode |
| 56 | + bashio::log.info "CPU supports AVX2 - using regular OpenCode binary" |
| 57 | + elif [ "${has_avx}" = "true" ]; then |
| 58 | + echo "baseline" > /data/.cpu_mode |
| 59 | + bashio::log.info "CPU has AVX but not AVX2 - using baseline OpenCode binary" |
| 60 | + else |
| 61 | + echo "baseline" > /data/.cpu_mode |
| 62 | + bashio::log.warning "CPU lacks AVX support - using baseline OpenCode binary (limited functionality)" |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +# Run CPU detection |
| 67 | +detect_cpu_capabilities |
| 68 | + |
| 69 | +# ============================================================================== |
| 70 | +# Install/Verify OpenCode binary based on CPU capabilities |
| 71 | +# ============================================================================== |
| 72 | + |
| 73 | +install_opencode_binary() { |
| 74 | + local cpu_mode |
| 75 | + cpu_mode=$(cat /data/.cpu_mode 2>/dev/null || echo "unknown") |
| 76 | + |
| 77 | + # Determine the correct package based on architecture and CPU mode |
| 78 | + local opencode_package |
| 79 | + local arch |
| 80 | + |
| 81 | + # Detect architecture |
| 82 | + case $(uname -m) in |
| 83 | + x86_64) |
| 84 | + arch="x64" |
| 85 | + ;; |
| 86 | + aarch64|arm64) |
| 87 | + arch="arm64" |
| 88 | + ;; |
| 89 | + *) |
| 90 | + bashio::log.warning "Unknown architecture: $(uname -m), using x64" |
| 91 | + arch="x64" |
| 92 | + ;; |
| 93 | + esac |
| 94 | + |
| 95 | + # Select appropriate package based on CPU mode |
| 96 | + if [ "${cpu_mode}" = "baseline" ]; then |
| 97 | + # Check if baseline is already installed |
| 98 | + if [ -d "/usr/local/lib/node_modules/opencode-linux-${arch}-baseline" ]; then |
| 99 | + bashio::log.info "OpenCode baseline binary already installed" |
| 100 | + else |
| 101 | + bashio::log.info "Installing OpenCode baseline binary for ${arch}..." |
| 102 | + npm install -g "opencode-linux-${arch}-baseline" --force 2>/dev/null || \ |
| 103 | + npm install -g opencode-ai --force 2>/dev/null || true |
| 104 | + bashio::log.info "OpenCode baseline binary installed" |
| 105 | + fi |
| 106 | + |
| 107 | + # Create wrapper to use baseline binary |
| 108 | + create_opencode_wrapper "baseline" |
| 109 | + else |
| 110 | + # Regular OpenCode (AVX2) |
| 111 | + if command -v opencode &>/dev/null; then |
| 112 | + bashio::log.info "OpenCode regular binary already installed" |
| 113 | + else |
| 114 | + bashio::log.info "Installing OpenCode regular binary..." |
| 115 | + npm install -g opencode-ai 2>/dev/null || true |
| 116 | + bashio::log.info "OpenCode regular binary installed" |
| 117 | + fi |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +# Create wrapper script that selects the right binary |
| 122 | +create_opencode_wrapper() { |
| 123 | + local mode="$1" |
| 124 | + local wrapper_path="/usr/local/bin/opencode" |
| 125 | + |
| 126 | + # Backup original if exists and not a wrapper |
| 127 | + if [ -f "${wrapper_path}" ] && [ ! -L "${wrapper_path}" ]; then |
| 128 | + # Check if it's already our wrapper |
| 129 | + if ! grep -q "CPU_MODE" "${wrapper_path}" 2>/dev/null; then |
| 130 | + mv "${wrapper_path}" "${wrapper_path}.original" |
| 131 | + fi |
| 132 | + fi |
| 133 | + |
| 134 | + # Create wrapper script |
| 135 | + cat > "${wrapper_path}" << 'EOF' |
| 136 | +#!/bin/bash |
| 137 | +# OpenCode wrapper - selects correct binary based on CPU capabilities |
| 138 | + |
| 139 | +CPU_MODE=$(cat /data/.cpu_mode 2>/dev/null || echo "regular") |
| 140 | +ARCH=$(uname -m) |
| 141 | + |
| 142 | +# For baseline mode, try to use baseline binary |
| 143 | +if [ "${CPU_MODE}" = "baseline" ]; then |
| 144 | + case "${ARCH}" in |
| 145 | + x86_64) |
| 146 | + BASELINE_BIN="/usr/local/lib/node_modules/opencode-linux-x64-baseline/bin/opencode" |
| 147 | + ;; |
| 148 | + aarch64|arm64) |
| 149 | + BASELINE_BIN="/usr/local/lib/node_modules/opencode-linux-arm64-baseline/bin/opencode" |
| 150 | + ;; |
| 151 | + esac |
| 152 | + |
| 153 | + if [ -x "${BASELINE_BIN}" ]; then |
| 154 | + exec "${BASELINE_BIN}" "$@" |
| 155 | + fi |
| 156 | +fi |
| 157 | + |
| 158 | +# Fallback to regular/opencode-ai |
| 159 | +if [ -x "/usr/local/lib/node_modules/opencode-ai/bin/opencode" ]; then |
| 160 | + exec /usr/local/lib/node_modules/opencode-ai/bin/opencode "$@" |
| 161 | +fi |
| 162 | + |
| 163 | +# Last resort - try system opencode |
| 164 | +exec opencode "$@" |
| 165 | +EOF |
| 166 | + |
| 167 | + chmod +x "${wrapper_path}" |
| 168 | + bashio::log.info "Created OpenCode wrapper for ${mode} mode" |
| 169 | +} |
| 170 | + |
| 171 | +# Run OpenCode installation |
| 172 | +install_opencode_binary |
| 173 | + |
9 | 174 | # Set up persistent HOME for all services |
10 | 175 | export HOME="/data" |
11 | 176 | export XDG_DATA_HOME="/data/.local/share" |
|
41 | 206 | # Read configuration options |
42 | 207 | # ============================================================================== |
43 | 208 |
|
44 | | -MCP_ENABLED=$(bashio::config 'mcp_enabled') |
45 | | -LSP_ENABLED=$(bashio::config 'lsp_enabled' || echo "true") |
46 | | - |
47 | | -bashio::log.info "Configuration: MCP=${MCP_ENABLED} LSP=${LSP_ENABLED}" |
| 209 | +bashio::log.info "Configuration: MCP=${MCP_ENABLED} LSP=${LSP_ENABLED} CPU_MODE=${CPU_MODE_CONFIG}" |
48 | 210 |
|
49 | 211 | # ============================================================================== |
50 | 212 | # Generate OpenCode configuration file |
|
0 commit comments