-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_zclaude.sh
More file actions
executable file
Β·520 lines (453 loc) Β· 18.1 KB
/
Copy pathsetup_zclaude.sh
File metadata and controls
executable file
Β·520 lines (453 loc) Β· 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/bin/bash
# zclaude Setup Script
# This script installs Claude CLI and configures it with Z.AI MCP servers
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color
# ASCII Art Banner
echo -e "\033[38;5;250m" # Silver/gray color for Z.AI
cat << "EOF"
βββββββββββ βββββββββ βββββ βββββββββ βββββββββ
βββββββββββ βββββββββββ βββββ βββ βββββββββββ βββββββββββ
β ββββ ββββ ββββ ββββ ββββ βββ βββ βββ βββ
βββ ββββββββββββ ββββ βββββββββββ ββββ ββββ
βββ ββββββββββββ ββββ βββββββββββ ββββ ββββ
ββββ β ββββ ββββ ββββ ββββ βββββ ββββββββ βββ
βββββββββββ ββ βββββ βββββ βββββ βββ βββββββββββ βββββββββββ
βββββββββββ ββ βββββ βββββ βββββ βββββββββ βββββββββ
EOF
echo -e "\033[0m\033[38;5;208m" # Orange color for Claude Code
cat << "EOF"
β€οΈ Claude Code
EOF
echo -e "\033[0m"
echo -e "${BLUE}π Welcome to the Z.AI Coding Plan β€οΈ Claude Code Dual Setup Script!${NC}"
echo
echo -e "${YELLOW}π― Problem This Solves:${NC}"
echo " β’ Tired of editing ~/.claude/settings.json to switch providers?"
echo " β’ Want to use both Claude Pro AND Z.AI Coding Plan with claude code client?"
echo " β’ Need quick access to both without complex Docker setups?"
echo
echo -e "${GREEN}π‘ The Solution:${NC}"
echo " This script gives you TWO separate commands:"
echo -e " β’ ${CYAN}claude${NC} - Uses your Claude Code Pro subscription"
echo -e " β’ ${CYAN}zclaude${NC} - Uses Z.AI's coding plan"
echo
echo -e "${YELLOW}π What We'll Set Up (Step-by-Step):${NC}"
echo " 1. β
Install Claude CLI if needed (or verify existing installation)"
echo " 2. π Configure your Anthropic Auth Token for Z.AI access"
echo " 3. π Add 'zclaude' alias to your shell (keeps default 'claude' untouched)"
echo " 4. π Configure Z.AI MCP servers for enhanced functionality"
echo
echo -e "${GREEN}π End Result:${NC}"
echo -e " β’ ${CYAN}claude --help${NC} β Your regular Claude Code Pro"
echo -e " β’ ${CYAN}zclaude --help${NC} β Z.AI's coding plan"
echo " β’ No more configuration file editing!"
echo
echo
# Function to print colored status messages
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to detect user's actual default shell
detect_shell() {
local detected_shell=""
# Method 1: Check the user's SHELL environment variable (this is the user's default shell)
if [[ -n "$SHELL" ]]; then
case "$(basename "$SHELL")" in
"zsh")
detected_shell="zsh"
;;
"bash")
detected_shell="bash"
;;
"fish")
detected_shell="fish"
;;
"ksh")
detected_shell="ksh"
;;
"csh"|"tcsh")
detected_shell="csh"
;;
*)
detected_shell="unknown"
;;
esac
fi
# Method 2: If SHELL variable is not set or unknown, check /etc/passwd for user's shell
if [[ -z "$detected_shell" || "$detected_shell" == "unknown" ]]; then
if command -v getent &> /dev/null; then
local user_shell=$(getent passwd "$(whoami)" 2>/dev/null | cut -d: -f7)
if [[ -n "$user_shell" ]]; then
case "$(basename "$user_shell")" in
"zsh")
detected_shell="zsh"
;;
"bash")
detected_shell="bash"
;;
"fish")
detected_shell="fish"
;;
"ksh")
detected_shell="ksh"
;;
"csh"|"tcsh")
detected_shell="csh"
;;
*)
detected_shell="unknown"
;;
esac
fi
fi
fi
# Method 3: Check for existing config files as last resort
if [[ -z "$detected_shell" || "$detected_shell" == "unknown" ]]; then
if [[ -f "$HOME/.zshrc" ]]; then
detected_shell="zsh"
elif [[ -f "$HOME/.bashrc" ]]; then
detected_shell="bash"
elif [[ -f "$HOME/.config/fish/config.fish" ]]; then
detected_shell="fish"
elif [[ -f "$HOME/.kshrc" ]]; then
detected_shell="ksh"
else
detected_shell="bash" # Default to bash as most common
fi
fi
echo "$detected_shell"
}
# Function to get rc file path based on shell
get_rc_file() {
local shell_type=$1
case $shell_type in
"zsh")
echo "$HOME/.zshrc"
;;
"bash")
# Check for .bash_profile first, then .bashrc
if [[ -f "$HOME/.bash_profile" ]]; then
echo "$HOME/.bash_profile"
elif [[ -f "$HOME/.profile" ]] && [[ ! -f "$HOME/.bashrc" ]]; then
echo "$HOME/.profile"
else
echo "$HOME/.bashrc"
fi
;;
"fish")
echo "$HOME/.config/fish/config.fish"
;;
"ksh")
echo "$HOME/.kshrc"
;;
"csh"|"tcsh")
echo "$HOME/.cshrc"
;;
*)
# Fallback to .profile for unknown shells
echo "$HOME/.profile"
;;
esac
}
# Function to create directory if it doesn't exist (for fish config)
ensure_config_dir() {
local shell_type=$1
if [[ "$shell_type" == "fish" ]]; then
mkdir -p "$HOME/.config/fish"
fi
}
# Function to remove existing zclaude configuration
remove_existing_zclaude_config() {
local rc_file=$1
if [[ -f "$rc_file" ]]; then
print_status "Removing existing zclaude configuration..."
# Remove the entire Z.AI Claude configuration block (works for all shells)
sed -i '/# Z.AI Claude Configuration - Added on/,/# End Z.AI Claude Configuration/d' "$rc_file" 2>/dev/null || true
# Remove bash/zsh/ksh style function
sed -i '/^zclaude_env()[[:space:]]*{/,/^}/d' "$rc_file" 2>/dev/null || true
# Remove fish shell function
sed -i '/^function zclaude_env$/,/^end$/d' "$rc_file" 2>/dev/null || true
# Remove csh/tcsh style aliases
sed -i '/^alias[[:space:]]*zclaude_env[[:space:]]/d' "$rc_file" 2>/dev/null || true
sed -i '/^alias[[:space:]]*zclaude[[:space:]]/d' "$rc_file" 2>/dev/null || true
sed -i '/^alias zclaude_env=/d' "$rc_file" 2>/dev/null || true
sed -i '/^alias zclaude=/d' "$rc_file" 2>/dev/null || true
# Remove fish style aliases (alias zclaude=zclaude_env)
sed -i '/^alias[[:space:]]*zclaude[[:space:]]*=zclaude_env/d' "$rc_file" 2>/dev/null || true
print_success "Existing configuration removed"
fi
}
# Function to remove existing MCP servers
remove_existing_mcp_servers() {
print_status "Removing existing Z.AI MCP servers..."
local servers=("zai-mcp-server" "web-search-prime" "web-reader")
for server in "${servers[@]}"; do
if claude mcp list 2>/dev/null | grep -q "$server"; then
print_status "Removing existing MCP server: $server"
if claude mcp remove "$server" 2>/dev/null; then
print_success "β
Removed $server"
else
print_warning "β οΈ Could not remove $server (may not exist)"
fi
fi
done
}
# Function to attempt manual Claude installation
manual_claude_installation() {
print_warning "npm installation failed. Attempting alternative installation methods..."
# Check if we have curl/wget for downloading
if command -v curl &> /dev/null || command -v wget &> /dev/null; then
print_status "Attempting to download Claude CLI binary..."
print_status "Please visit https://docs.claude.com for manual installation instructions"
print_status "After manual installation, please re-run this script"
echo
echo -e "${YELLOW}Manual installation steps:${NC}"
echo "1. Visit: https://docs.claude.com/en/docs/claude-code/installation"
echo "2. Download the appropriate binary for your system"
echo "3. Place it in your PATH (e.g., /usr/local/bin/claude)"
echo "4. Make it executable: chmod +x /path/to/claude"
echo "5. Re-run this script: ./setup_zclaude.sh"
echo
exit 0
else
print_error "Neither curl nor wget available. Cannot attempt manual download."
exit 1
fi
}
# Step 1: Check if Claude is installed
print_status "Step 1: Checking Claude CLI installation..."
print_status "This ensures you have the base Claude CLI tool installed"
print_status "Your existing 'claude' command configuration will NOT be modified"
if ! command -v claude &> /dev/null; then
print_warning "Claude CLI not found in PATH. Installing now..."
if command -v npm &> /dev/null; then
print_status "Installing Claude CLI using npm..."
if npm install -g @anthropic-ai/claude-code; then
# Verify installation
if command -v claude &> /dev/null; then
print_success "Claude CLI installed successfully!"
else
print_error "Claude CLI installation completed but command not found in PATH."
manual_claude_installation
fi
else
print_error "Failed to install Claude CLI via npm."
manual_claude_installation
fi
else
print_error "npm not found. Cannot attempt automatic installation."
manual_claude_installation
fi
else
print_success "Claude CLI is already installed! β
"
fi
# Step 2: Get Anthropic Auth Token
print_status "Step 2: Setting up Anthropic Auth Token for Z.AI access..."
print_status "This token will be used ONLY for the 'zclaude' command"
print_status "Your existing 'claude' command will continue using its current configuration"
MAX_ATTEMPTS=3
attempt=1
while [[ $attempt -le $MAX_ATTEMPTS ]]; do
echo -n -e "${CYAN}Please enter your Anthropic Auth Token (attempt $attempt/$MAX_ATTEMPTS): ${NC}"
read -r -s ANTHROPIC_TOKEN
echo
if [[ -z "$ANTHROPIC_TOKEN" ]]; then
remaining_attempts=$((MAX_ATTEMPTS - attempt))
if [[ $remaining_attempts -gt 0 ]]; then
print_error "Auth token cannot be empty. $remaining_attempts attempts remaining."
else
print_error "Auth token cannot be empty. No attempts remaining."
fi
((attempt++))
else
print_success "Auth token received! π"
break
fi
done
if [[ $attempt -gt $MAX_ATTEMPTS ]]; then
print_error "Maximum attempts reached. Exiting setup."
exit 1
fi
# Step 3: Configure shell
print_status "Step 3: Adding 'zclaude' command to your shell..."
print_status "This creates a NEW command that doesn't interfere with your existing 'claude' command"
SHELL_TYPE=$(detect_shell)
RC_FILE=$(get_rc_file "$SHELL_TYPE")
print_status "Detected shell: $SHELL_TYPE"
print_status "Configuration file: $RC_FILE"
print_status "Creating backup before making changes..."
# Create backup of existing rc file
if [[ -f "$RC_FILE" ]]; then
BACKUP_FILE="${RC_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
cp "$RC_FILE" "$BACKUP_FILE"
print_status "Created backup: $BACKUP_FILE"
# Remove existing zclaude configuration
remove_existing_zclaude_config "$RC_FILE"
fi
# Add zclaude function and alias to rc file based on shell type
add_shell_config() {
local shell_type=$1
local rc_file=$2
case "$shell_type" in
"bash"|"zsh"|"ksh")
cat >> "$rc_file" << EOF
# Z.AI Claude Configuration - Added on $(date)
zclaude_env() {
ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic" \\
API_TIMEOUT_MS="3000000" \\
ANTHROPIC_DEFAULT_HAIKU_MODEL="glm-4.5-air" \\
ANTHROPIC_DEFAULT_SONNET_MODEL="glm-4.6" \\
ANTHROPIC_DEFAULT_OPUS_MODEL="glm-4.6" \\
ANTHROPIC_AUTH_TOKEN="$ANTHROPIC_TOKEN" \\
claude "\$@"
}
alias zclaude="zclaude_env"
# End Z.AI Claude Configuration
EOF
;;
"fish")
cat >> "$rc_file" << EOF
# Z.AI Claude Configuration - Added on $(date)
function zclaude_env
set -gx ANTHROPIC_BASE_URL "https://api.z.ai/api/anthropic"
set -gx API_TIMEOUT_MS "3000000"
set -gx ANTHROPIC_DEFAULT_HAIKU_MODEL "glm-4.5-air"
set -gx ANTHROPIC_DEFAULT_SONNET_MODEL "glm-4.6"
set -gx ANTHROPIC_DEFAULT_OPUS_MODEL "glm-4.6"
set -gx ANTHROPIC_AUTH_TOKEN "$ANTHROPIC_TOKEN"
claude \$argv
end
alias zclaude=zclaude_env
# End Z.AI Claude Configuration
EOF
;;
"csh"|"tcsh")
cat >> "$rc_file" << EOF
# Z.AI Claude Configuration - Added on $(date)
alias zclaude_env 'setenv ANTHROPIC_BASE_URL "https://api.z.ai/api/anthropic"; \
setenv API_TIMEOUT_MS "3000000"; \
setenv ANTHROPIC_DEFAULT_HAIKU_MODEL "glm-4.5-air"; \
setenv ANTHROPIC_DEFAULT_SONNET_MODEL "glm-4.6"; \
setenv ANTHROPIC_DEFAULT_OPUS_MODEL "glm-4.6"; \
setenv ANTHROPIC_AUTH_TOKEN "$ANTHROPIC_TOKEN"; \
claude \!*'
alias zclaude zclaude_env
# End Z.AI Claude Configuration
EOF
;;
*)
# Generic shell configuration (POSIX compliant)
cat >> "$rc_file" << EOF
# Z.AI Claude Configuration - Added on $(date)
zclaude_env() {
ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic" \
API_TIMEOUT_MS="3000000" \
ANTHROPIC_DEFAULT_HAIKU_MODEL="glm-4.5-air" \
ANTHROPIC_DEFAULT_SONNET_MODEL="glm-4.6" \
ANTHROPIC_DEFAULT_OPUS_MODEL="glm-4.6" \
ANTHROPIC_AUTH_TOKEN="$ANTHROPIC_TOKEN" \
claude "\$@"
}
alias zclaude="zclaude_env"
# End Z.AI Claude Configuration
EOF
;;
esac
}
# Ensure config directory exists if needed
ensure_config_dir "$SHELL_TYPE"
# Add zclaude configuration to the appropriate rc file
add_shell_config "$SHELL_TYPE" "$RC_FILE"
print_success "Shell configuration added successfully! π"
# Step 4: Add MCP servers
print_status "Step 4: Configuring Z.AI MCP servers for enhanced functionality..."
print_status "These provide additional capabilities like web search and reading for 'zclaude'"
# Remove existing MCP servers first
remove_existing_mcp_servers
# Add MCP servers with the user's token
print_status "Adding zai-mcp-server..."
if claude mcp add -s user zai-mcp-server --env Z_AI_API_KEY="$ANTHROPIC_TOKEN" Z_AI_MODE=ZAI -- npx -y "@z_ai/mcp-server"; then
print_success "β
zai-mcp-server added successfully"
else
print_warning "β οΈ Failed to add zai-mcp-server"
fi
print_status "Adding web-search-prime MCP server..."
if claude mcp add -s user -t http web-search-prime "https://api.z.ai/api/mcp/web_search_prime/mcp" --header "Authorization: Bearer $ANTHROPIC_TOKEN"; then
print_success "β
web-search-prime added successfully"
else
print_warning "β οΈ Failed to add web-search-prime MCP server"
fi
print_status "Adding web-reader MCP server..."
if claude mcp add -s user -t http web-reader "https://api.z.ai/api/mcp/web_reader/mcp" --header "Authorization: Bearer $ANTHROPIC_TOKEN"; then
print_success "β
web-reader added successfully"
else
print_warning "β οΈ Failed to add web-reader MCP server"
fi
# Show final MCP server list
print_status "Current MCP servers:"
claude mcp list 2>/dev/null || print_warning "Could not list MCP servers"
# Final instructions
echo
echo -e "${GREEN}π Setup completed successfully!${NC}"
echo
echo
echo -e "${GREEN}π Setup completed successfully!${NC}"
echo
echo -e "${PURPLE}=== How to Use Your Dual Setup ===${NC}"
echo
echo -e "${YELLOW}π Load the new configuration:${NC}"
echo -e "${YELLOW}1. Restart your terminal OR run:${NC}"
# Shell-specific source command
case "$SHELL_TYPE" in
"fish")
echo -e "${CYAN} source $RC_FILE${NC}"
echo -e "${CYAN} # OR: fish -C \"source $RC_FILE\"${NC}"
;;
"csh"|"tcsh")
echo -e "${CYAN} source $RC_FILE${NC}"
;;
*)
echo -e "${CYAN} source $RC_FILE${NC}"
;;
esac
echo
echo -e "${YELLOW}π Start using both commands:${NC}"
echo -e "${GREEN} claude \"Your professional task\"${NC} ${GRAY}# Uses your Claude Code Pro${NC}"
echo -e "${CYAN} zclaude \"Your coding project\"${NC} ${GRAY}# Uses Z.AI enhanced features${NC}"
echo
echo -e "${YELLOW}π‘ Quick Test:${NC}"
echo -e "${CYAN} claude --version${NC} # Check your regular Claude"
echo -e "${CYAN} zclaude --version${NC} # Check Z.AI version"
echo -e "${CYAN} claude mcp list${NC} # See MCP servers"
echo
echo -e "${PURPLE}=== What You Now Have ===${NC}"
echo -e "${GREEN}β
${NC} Dual command setup (no more config file editing!)"
echo -e "${GREEN}β
${NC} Z.AI higher limits on models: glm-4.5-air, glm-4.6"
echo -e "${GREEN}β
${NC} Web search via web-search-prime MCP server"
echo -e "${GREEN}β
${NC} Web page reading via web-reader MCP server"
echo -e "${GREEN}β
${NC} Seamless switching between Claude Code Pro and Z.AI Coding Plan"
echo
echo -e "${GREEN}Enjoy using zclaude! π${NC}"
echo
echo -e "${BLUE}If you encounter any issues, please:${NC}"
echo "β’ Check your internet connection"
echo "β’ Verify your API token is correct"
echo "β’ Ensure all dependencies are installed"