Skip to content

Commit 855472e

Browse files
author
Oleg
committed
feat: add CPU baseline detection for older processors
- Add CPU capability detection (AVX2/AVX) in init-opencode script - Auto-select baseline OpenCode binary for CPUs without AVX2 - Add cpu_mode config option (auto|baseline|regular) - Display CPU mode in terminal banner - Create wrapper script to select correct binary at runtime Fixes: anomalyco/opencode#12553
1 parent de7520d commit 855472e

4 files changed

Lines changed: 6278 additions & 5 deletions

File tree

‎ha_opencode/config.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ options:
4545
mcp_enabled: false
4646
lsp_enabled: true
4747

48+
# CPU mode: auto (detect), baseline (for older CPUs), regular (force AVX2)
49+
cpu_mode: "auto"
50+
4851
# Terminal appearance
4952
terminal_theme: "breeze"
5053
font_size: 14
@@ -65,6 +68,9 @@ schema:
6568
mcp_enabled: bool
6669
lsp_enabled: bool
6770

71+
# CPU mode
72+
cpu_mode: list(auto|baseline|regular)
73+
6874
# Terminal appearance
6975
terminal_theme: list(breeze|catppuccin_mocha|catppuccin_latte|dracula|nord|tokyo_night|one_dark|solarized_dark|solarized_light|gruvbox_dark)
7076
font_size: int(10,24)

‎ha_opencode/rootfs/etc/s6-overlay/s6-rc.d/init-opencode/run‎

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,171 @@
66

77
bashio::log.info "Initializing HA OpenCode..."
88

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+
9174
# Set up persistent HOME for all services
10175
export HOME="/data"
11176
export XDG_DATA_HOME="/data/.local/share"
@@ -41,10 +206,7 @@ fi
41206
# Read configuration options
42207
# ==============================================================================
43208

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}"
48210

49211
# ==============================================================================
50212
# Generate OpenCode configuration file

‎ha_opencode/rootfs/usr/local/bin/opencode-session.sh‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ GRAY='\\033[38;2;127;140;141m'
2828
BOLD='\\033[1m'
2929
NC='\\033[0m'
3030

31+
CPU_MODE=$(cat /data/.cpu_mode 2>/dev/null || echo "unknown")
32+
CPU_INFO=""
33+
if [ "${CPU_MODE}" = "baseline" ]; then
34+
CPU_INFO=" ${YELLOW}(baseline CPU mode)${NC}"
35+
fi
36+
3137
# Change to Home Assistant config directory
3238
cd /homeassistant
3339

@@ -44,7 +50,7 @@ fi
4450
show_banner() {
4551
clear
4652
echo ""
47-
echo -e "${BLUE}${BOLD}HA OpenCode${NC} ${GRAY}v1.0${NC}"
53+
echo -e "${BLUE}${BOLD}HA OpenCode${NC} ${GRAY}v1.0${NC}${CPU_INFO}"
4854
echo -e "${GRAY}AI-powered coding agent for Home Assistant${NC}"
4955
echo ""
5056
echo -e "${GRAY}────────────────────────────────────────────────────────────${NC}"

0 commit comments

Comments
 (0)