Refactor: Simplify coding-helper installer section

Simplified the approach to use npx @z_ai/coding-helper directly instead
of complex component detection. Removed 200+ lines of unnecessary code.

Changes:
- Remove detect_coding_helper_components() (10-component JSON parsing)
- Remove display_component_status() (unused display function)
- Remove check_coding_helper_installed() (duplicated logic)
- Remove install_coding_helper() (old complex version)
- Remove launch_coding_helper() (wrapper function)
- Keep offer_coding_helper_addons() with simple check
- Keep install_coding_helper_simple() with EACCES handling

This addresses the "0/10 components installed" bug where complex
grep/cut JSON parsing was failing in subshell context.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
uroma
2026-01-22 16:57:02 +00:00
Unverified
parent 3fe8e7711a
commit 9bdc5ff020

View File

@@ -482,172 +482,36 @@ EOF
fi fi
} }
check_coding_helper_installed() {
if command -v chelper &> /dev/null || npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
return 0
else
return 1
fi
}
# Detect coding-helper components and their installation status
detect_coding_helper_components() {
local status_json="{"
# 1. Core coding-helper installation
if command -v chelper &> /dev/null || npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
status_json+='"core":true,'
else
status_json+='"core":false,'
fi
# 2. Interactive wizard (part of core, but check if wizard can run)
if command -v chelper &> /dev/null || npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
status_json+='"wizard":true,'
else
status_json+='"wizard":false,'
fi
# 3. Multi-tool management (check for chelper config)
if [ -f "$HOME/.chelper/config.json" ] || [ -d "$HOME/.chelper" ]; then
status_json+='"multi_tool":true,'
else
status_json+='"multi_tool":false,'
fi
# 4. MCP service configuration (check for MCP configs in Claude settings)
if [ -f "$CLAUDE_DIR/settings.json" ]; then
if grep -q "mcpServers" "$CLAUDE_DIR/settings.json" 2>/dev/null || \
grep -q "mcp.*servers" "$CLAUDE_DIR/settings.json" 2>/dev/null; then
status_json+='"mcp_config":true,'
else
status_json+='"mcp_config":false,'
fi
else
status_json+='"mcp_config":false,'
fi
# 5. API key management (check for Z.AI API keys in settings)
if [ -f "$CLAUDE_DIR/settings.json" ]; then
if grep -q "dashscope-intl.aliyuncs.com" "$CLAUDE_DIR/settings.json" 2>/dev/null; then
status_json+='"api_management":true,'
else
status_json+='"api_management":false,'
fi
else
status_json+='"api_management":false,'
fi
# 6. Bilingual interface (inherent to coding-helper)
if command -v chelper &> /dev/null || npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
status_json+='"bilingual":true,'
else
status_json+='"bilingual":false,'
fi
# 7. MCP servers integration (check for installed MCP servers)
local mcp_count=0
if [ -d "$CLAUDE_DIR/plugins" ]; then
mcp_count=$(find "$CLAUDE_DIR/plugins" -name "*mcp*" -o -name "*zai*" 2>/dev/null | wc -l)
fi
if [ "$mcp_count" -gt 0 ]; then
status_json+='"mcp_servers":true,'
else
status_json+='"mcp_servers":false,'
fi
# 8. Plugin marketplace (check for marketplace config)
if [ -f "$CLAUDE_DIR/config.json" ] && grep -q "marketplace" "$CLAUDE_DIR/config.json" 2>/dev/null; then
status_json+='"marketplace":true,'
else
status_json+='"marketplace":false,'
fi
# 9. Tool detection (check for tool-discovery-agent skill)
if [ -f "$CLAUDE_DIR/skills/tool-discovery-agent/SKILL.md" ]; then
status_json+='"tool_detection":true,'
else
status_json+='"tool_detection":false,'
fi
# 10. Configuration backup/sync (check for sync scripts)
if [ -f "$CLAUDE_DIR/scripts/sync-agents.sh" ] || [ -f "$CLAUDE_DIR/agents/export-claude-customizations.sh" ]; then
status_json+='"backup_sync":true'
else
status_json+='"backup_sync":false'
fi
status_json+="}"
echo "$status_json"
}
# Display component status with colored indicators
display_component_status() {
local component_name="$1"
local is_installed="$2"
local indent="${3:- }"
if [ "$is_installed" = "true" ]; then
echo -e "${indent}${GREEN}${NC} ${component_name} ${GREEN}[Installed]${NC}"
else
echo -e "${indent}${RED}${NC} ${component_name} ${YELLOW}[Not installed]${NC}"
fi
}
offer_coding_helper_addons() { offer_coding_helper_addons() {
log_step "Z.AI Coding-Helper Addons" log_step "Z.AI Coding-Helper Addons"
echo "" echo ""
# Detect installed components
local status_json=$(detect_coding_helper_components)
# Parse JSON using simple string matching (bash compatible)
local core=$(echo "$status_json" | grep -o '"core":[^,}]*' | cut -d: -f2)
local wizard=$(echo "$status_json" | grep -o '"wizard":[^,}]*' | cut -d: -f2)
local multi_tool=$(echo "$status_json" | grep -o '"multi_tool":[^,}]*' | cut -d: -f2)
local mcp_config=$(echo "$status_json" | grep -o '"mcp_config":[^,}]*' | cut -d: -f2)
local api_management=$(echo "$status_json" | grep -o '"api_management":[^,}]*' | cut -d: -f2)
local bilingual=$(echo "$status_json" | grep -o '"bilingual":[^,}]*' | cut -d: -f2)
local mcp_servers=$(echo "$status_json" | grep -o '"mcp_servers":[^,}]*' | cut -d: -f2)
local marketplace=$(echo "$status_json" | grep -o '"marketplace":[^,}]*' | cut -d: -f2)
local tool_detection=$(echo "$status_json" | grep -o '"tool_detection":[^,}]*' | cut -d: -f2)
local backup_sync=$(echo "$status_json" | grep -o '"backup_sync":[^,}]*' | cut -d: -f2)
echo -e "${CYAN}Z.AI's coding-helper (chelper) provides additional features:${NC}" echo -e "${CYAN}Z.AI's coding-helper (chelper) provides additional features:${NC}"
echo "" echo ""
display_component_status "Interactive wizard for easy setup" "$wizard" echo " • Interactive wizard for easy setup"
display_component_status "Multi-tool management (Claude Code, OpenCode, etc.)" "$multi_tool" echo " • Multi-tool management (Claude Code, OpenCode, etc.)"
display_component_status "MCP service configuration" "$mcp_config" echo " • MCP service configuration"
display_component_status "API key management for both Global and China plans" "$api_management" echo " • API key management for both Global and China plans"
display_component_status "Bilingual interface (English/Chinese)" "$bilingual" echo " • Bilingual interface (English/Chinese)"
echo "" echo ""
echo -e "${YELLOW}Available addons/features:${NC}" echo -e "${YELLOW}Available addons/features:${NC}"
display_component_status "MCP servers integration (Blender, filesystem, etc.)" "$mcp_servers" echo " • MCP servers integration (Blender, filesystem, etc.)"
display_component_status "Plugin marketplace access" "$marketplace" echo " • Plugin marketplace access"
display_component_status "Automatic tool detection and installation" "$tool_detection" echo " • Automatic tool detection and installation"
display_component_status "Configuration backup and sync" "$backup_sync" echo " • Configuration backup and sync"
echo "" echo ""
# Calculate overall status # Check if chelper command exists or npm package is installed
local total=10 local is_installed=false
local installed=0 if command -v chelper &> /dev/null; then
[ "$core" = "true" ] && ((installed++)) is_installed=true
[ "$wizard" = "true" ] && ((installed++)) elif npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
[ "$multi_tool" = "true" ] && ((installed++)) is_installed=true
[ "$mcp_config" = "true" ] && ((installed++)) fi
[ "$api_management" = "true" ] && ((installed++))
[ "$bilingual" = "true" ] && ((installed++))
[ "$mcp_servers" = "true" ] && ((installed++))
[ "$marketplace" = "true" ] && ((installed++))
[ "$tool_detection" = "true" ] && ((installed++))
[ "$backup_sync" = "true" ] && ((installed++))
echo -e "${CYAN}Installation Status:${NC} ${GREEN}${installed}${NC}/${total} components installed" if [ "$is_installed" = true ]; then
log_success "coding-helper is installed!"
echo "" echo ""
echo -e "${CYAN}You can run it anytime with:${NC} ${YELLOW}npx @z_ai/coding-helper${NC}"
# If everything is installed, offer to launch wizard or skip
if [ "$installed" -eq "$total" ]; then
log_success "All coding-helper components are already installed!"
echo "" echo ""
echo -e "${CYAN}Launch coding-helper wizard now?${NC} ${GREEN}[Y/n]${NC}" echo -e "${CYAN}Launch coding-helper wizard now?${NC} ${GREEN}[Y/n]${NC}"
>&2 echo -en "\033[0;36m> \033[0m" >&2 echo -en "\033[0;36m> \033[0m"
@@ -656,66 +520,39 @@ offer_coding_helper_addons() {
set -e set -e
echo "" echo ""
if [[ $launch_helper =~ ^[Yy]$ ]]; then if [[ $launch_helper =~ ^[Yy]$ ]]; then
launch_coding_helper npx @z_ai/coding-helper
fi fi
return return
fi fi
# If core is installed but some components missing, ask to install # Not installed - offer installation
if [ "$core" = "true" ]; then
log_info "coding-helper is installed, but some components may be missing"
echo ""
echo -e "${CYAN}Launch coding-helper wizard to configure missing components?${NC} ${GREEN}[Y/n]${NC}"
>&2 echo -en "\033[0;36m> \033[0m"
set +e
read -n 1 -r launch_helper < /dev/tty 2>/dev/null || launch_helper="N"
set -e
echo ""
if [[ $launch_helper =~ ^[Yy]$ ]]; then
launch_coding_helper
fi
return
fi
# Core not installed, offer installation
echo "" echo ""
echo -e "${BOLD}${MAGENTA}═══════════════════════════════════════════════════════════${NC}" echo -e "${BOLD}${MAGENTA}═══════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}${MAGENTA} Install Z.AI coding-helper now?${NC} ${GREEN}[Y/n]${NC}" echo -e "${BOLD}${MAGENTA} Install Z.AI coding-helper now?${NC} ${GREEN}[Y/n]${NC}"
echo -e "${BOLD}${MAGENTA}═══════════════════════════════════════════════════════════${NC}" echo -e "${BOLD}${MAGENTA}═══════════════════════════════════════════════════════════${NC}"
read -p " " -n 1 -r install_helper < /dev/tty set +e
read -n 1 -r install_helper < /dev/tty 2>/dev/null || install_helper="Y"
set -e
echo "" echo ""
# Default to Yes if user just presses Enter # Default to Yes if user just presses Enter or enters Y/y
if [[ -z "$install_helper" ]] || [[ $install_helper =~ ^[Yy]$ ]]; then if [[ -z "$install_helper" ]] || [[ $install_helper =~ ^[Yy]$ ]]; then
install_coding_helper install_coding_helper_simple
else else
log_info "Skipping coding-helper installation" log_info "Skipping coding-helper installation"
return echo ""
echo -e "${CYAN}You can install it later with:${NC}"
echo -e " ${YELLOW}npm install -g @z_ai/coding-helper${NC}"
echo " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi fi
} }
install_coding_helper() { # Simplified installation function
install_coding_helper_simple() {
log_info "Installing @z_ai/coding-helper..." log_info "Installing @z_ai/coding-helper..."
echo "" echo ""
if check_coding_helper_installed; then # Try regular install first
log_info "coding-helper already installed"
echo ""
# Show prompt explicitly (read -p doesn't work with curl | bash)
echo -e "${CYAN}Launch coding-helper wizard now?${NC} ${GREEN}[Y/n]${NC}"
>&2 echo -en "\033[0;36m> \033[0m"
# Temporarily disable set -e for read command
set +e
read -n 1 -r launch_helper < /dev/tty 2>/dev/null || launch_helper="N"
set -e
echo ""
if [[ $launch_helper =~ ^[Yy]$ ]]; then
launch_coding_helper
fi
return
fi
# Install the helper (temporarily disable set -e to capture errors)
set +e set +e
local install_output local install_output
install_output=$(npm install -g @z_ai/coding-helper 2>&1) install_output=$(npm install -g @z_ai/coding-helper 2>&1)
@@ -725,30 +562,27 @@ install_coding_helper() {
if [ $install_exit_code -eq 0 ]; then if [ $install_exit_code -eq 0 ]; then
# Verify installation # Verify installation
if npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then if npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
log_success "coding-helper installed and verified!" log_success "coding-helper installed!"
echo "" echo ""
echo -e "${CYAN}You can now run:${NC} ${YELLOW}npx @z_ai/coding-helper${NC}" echo -e "${CYAN}You can now run:${NC} ${YELLOW}npx @z_ai/coding-helper${NC}"
echo "" echo ""
echo -e "${CYAN}Launch coding-helper wizard now?${NC} ${GREEN}[Y/n]${NC}" echo -e "${CYAN}Launch the wizard now?${NC} ${GREEN}[Y/n]${NC}"
>&2 echo -en "\033[0;36m> \033[0m"
set +e set +e
read -n 1 -r launch_helper < /dev/tty 2>/dev/null || launch_helper="N" read -n 1 -r launch_helper < /dev/tty 2>/dev/null || launch_helper="Y"
set -e set -e
echo "" echo ""
if [[ $launch_helper =~ ^[Yy]$ ]]; then if [[ -z "$launch_helper" ]] || [[ $launch_helper =~ ^[Yy]$ ]]; then
launch_coding_helper npx @z_ai/coding-helper
fi fi
else else
log_warn "Installation appeared to succeed but verification failed" log_warn "Installation appeared to succeed but verification failed"
echo "" echo ""
echo -e "${YELLOW}Installation output:${NC}" echo -e "${CYAN}You can still run it with:${NC} ${YELLOW}npx @z_ai/coding-helper${NC}"
echo "$install_output"
echo ""
echo -e "${CYAN}You can try running manually:${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi fi
else return
# Check if it's a permission error (EACCES) fi
# Check if permission error
if echo "$install_output" | grep -q "EACCES\|permission denied"; then if echo "$install_output" | grep -q "EACCES\|permission denied"; then
log_error "Installation failed due to permissions!" log_error "Installation failed due to permissions!"
echo "" echo ""
@@ -760,49 +594,40 @@ install_coding_helper() {
echo -e "${GREEN}Option 1: Use sudo (recommended)${NC}" echo -e "${GREEN}Option 1: Use sudo (recommended)${NC}"
echo -e " Run: ${YELLOW}sudo npm install -g @z_ai/coding-helper${NC}" echo -e " Run: ${YELLOW}sudo npm install -g @z_ai/coding-helper${NC}"
echo "" echo ""
echo -e "${GREEN}Option 2: Fix npm permissions permanently${NC}" echo -e "${GREEN}Option 2: Run without installation (works immediately)${NC}"
echo -e " Run: ${YELLOW}sudo chown -R \$(whoami) /usr/local/lib/node_modules${NC}"
echo -e " ${YELLOW}sudo chown -R \$(whoami) /usr/local/bin${NC}"
echo -e " Then: ${YELLOW}npm install -g @z_ai/coding-helper${NC}"
echo ""
echo -e "${GREEN}Option 3: Run without installation (works immediately)${NC}"
echo -e " Just use: ${YELLOW}npx @z_ai/coding-helper${NC}" echo -e " Just use: ${YELLOW}npx @z_ai/coding-helper${NC}"
echo "" echo ""
echo -e "${BOLD}${YELLOW}═══════════════════════════════════════════════════════════${NC}" echo -e "${BOLD}${YELLOW}═══════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD} Try installing with sudo now?${NC} ${GREEN}[Y/n]${NC} ${BOLD}(recommended)${NC}" echo -e "${BOLD} Try installing with sudo now?${NC} ${GREEN}[Y/n]${NC} ${BOLD}(recommended)${NC}"
echo -e "${BOLD}${YELLOW}═══════════════════════════════════════════════════════════${NC}" echo -e "${BOLD}${YELLOW}═══════════════════════════════════════════════════════════${NC}"
# Temporarily disable set -e for read command
set +e set +e
read -p " " -n 1 -r use_sudo < /dev/tty 2>/dev/null || use_sudo="Y" read -n 1 -r use_sudo < /dev/tty 2>/dev/null || use_sudo="Y"
set -e set -e
echo "" echo ""
# Default to Yes if user just presses Enter
if [[ -z "$use_sudo" ]] || [[ $use_sudo =~ ^[Yy]$ ]]; then if [[ -z "$use_sudo" ]] || [[ $use_sudo =~ ^[Yy]$ ]]; then
echo "" echo ""
log_info "Installing with sudo..." log_info "Installing with sudo..."
set +e
if sudo npm install -g @z_ai/coding-helper 2>&1; then if sudo npm install -g @z_ai/coding-helper 2>&1; then
set -e
if npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then if npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
log_success "coding-helper installed and verified with sudo!" log_success "coding-helper installed!"
echo "" echo ""
# Show prompt explicitly (read -p doesn't work with curl | bash) echo -e "${CYAN}Launch the wizard now?${NC} ${GREEN}[Y/n]${NC}"
echo -e "${CYAN}Launch coding-helper wizard now?${NC} ${GREEN}[Y/n]${NC}"
echo -e "${CYAN}(Press Enter for Yes, or 'n' to skip)${NC}"
# Flush output to ensure prompt displays
>&2 echo -en "\033[0;36m> \033[0m"
set +e set +e
read -n 1 -r launch_helper < /dev/tty 2>/dev/null || launch_helper="Y" read -n 1 -r launch_helper < /dev/tty 2>/dev/null || launch_helper="Y"
set -e set -e
echo "" echo ""
echo ""
if [[ -z "$launch_helper" ]] || [[ $launch_helper =~ ^[Yy]$ ]]; then if [[ -z "$launch_helper" ]] || [[ $launch_helper =~ ^[Yy]$ ]]; then
launch_coding_helper npx @z_ai/coding-helper
fi fi
fi fi
else else
set -e
log_warn "Sudo installation also failed" log_warn "Sudo installation also failed"
echo "" echo ""
echo -e "${CYAN}You can still run it without installation:${NC}" echo -e "${CYAN}You can still run it with:${NC} ${YELLOW}npx @z_ai/coding-helper${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi fi
else else
echo "" echo ""
@@ -810,38 +635,13 @@ install_coding_helper() {
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}" echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi fi
else else
# Non-permission error # Other error
log_error "Installation failed!" log_error "Installation failed!"
echo "" echo ""
echo -e "${YELLOW}Error output:${NC}" echo -e "${YELLOW}Error output:${NC}"
echo "$install_output" echo "$install_output"
echo "" echo ""
echo -e "${CYAN}You can try installing manually:${NC}" echo -e "${CYAN}You can still run it with:${NC} ${YELLOW}npx @z_ai/coding-helper${NC}"
echo -e " ${YELLOW}npm install -g @z_ai/coding-helper${NC}"
echo -e " ${YELLOW}sudo npm install -g @z_ai/coding-helper${NC}"
echo ""
echo -e "${CYAN}Or run without installation:${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi
fi
}
launch_coding_helper() {
log_info "Launching Z.AI coding-helper wizard..."
echo ""
echo -e "${CYAN}The wizard will guide you through:${NC}"
echo " 1. Selecting UI language"
echo " 2. Choosing coding plan (Global/China)"
echo " 3. Entering API key"
echo " 4. Selecting tools to manage"
echo " 5. Installing and configuring tools"
echo " 6. Managing MCP services"
echo ""
if command -v chelper &> /dev/null; then
chelper
else
npx @z_ai/coding-helper
fi fi
} }