-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_claude.sh
More file actions
executable file
·398 lines (336 loc) · 11.8 KB
/
local_claude.sh
File metadata and controls
executable file
·398 lines (336 loc) · 11.8 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
#!/bin/bash
###############################################################################
# Local Claude Launcher - Routes Claude Code through LiteLLM Proxy
#
# This script sets up Anthropic environment variables to use a local
# LiteLLM proxy and launches Claude Code (the CLI tool).
#
# Usage:
# local_claude.sh [OPTIONS] [CLAUDE_ARGS]
#
# Examples:
# local_claude.sh # Launch Claude Code normally
# local_claude.sh --no-check # Skip health check
# local_claude.sh --verbose # Show environment variables
# local_claude.sh --help # Show this help
#
# You can add this to your PATH:
# export PATH="/home/p2enjoy/jupyterlab/vllm-server/launchers:$PATH"
#
###############################################################################
set -euo pipefail
# Get the directory where this script is located (works from any path)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# vars.env is in the parent directory (vllm-server root)
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
VARS_FILE="${PROJECT_DIR}/vars.env"
SECRETS_DIR="${SECRETS_DIR:-${PROJECT_DIR}/secrets}"
SECRETS_HELPER="${PROJECT_DIR}/scripts/load_secrets_env.sh"
# Default configuration
LITELLM_HOST="${LITELLM_HOST:-localhost}"
LITELLM_PORT="${LITELLM_PORT:-4000}"
PROXY_URL="http://${LITELLM_HOST}:${LITELLM_PORT}"
CHECK_HEALTH=true
VERBOSE=false
SHOW_HELP=false
DRY_RUN=false
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
###############################################################################
# Helper Functions
###############################################################################
log_info() {
echo -e "${BLUE}ℹ $1${NC}" >&2
}
log_success() {
echo -e "${GREEN}✓ $1${NC}" >&2
}
log_warning() {
echo -e "${YELLOW}⚠ $1${NC}" >&2
}
log_error() {
echo -e "${RED}✗ $1${NC}" >&2
}
require_arg() {
local flag="$1"
local value="${2:-}"
if [ -z "$value" ] || [[ "$value" == --* ]]; then
log_error "Missing value for ${flag}"
exit 1
fi
}
load_selected_vars() {
local file="$1"
while IFS= read -r line || [ -n "$line" ]; do
line="${line#"${line%%[![:space:]]*}"}"
[ -z "$line" ] && continue
[[ "$line" == \#* ]] && continue
[[ "$line" =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]] || continue
local key="${BASH_REMATCH[1]}"
local value="${BASH_REMATCH[2]}"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
if [[ "$value" =~ ^\".*\"$ ]] || [[ "$value" =~ ^\'.*\'$ ]]; then
value="${value:1:-1}"
fi
case "$key" in
ANTHROPIC_*|CLAUDE_CODE_*|HF_TOKEN|MODEL)
export "$key=$value"
;;
esac
done < "$file"
}
print_help() {
cat << 'EOF'
╔════════════════════════════════════════════════════════════════╗
║ Local Claude Launcher for LiteLLM Proxy ║
╚════════════════════════════════════════════════════════════════╝
DESCRIPTION:
Routes Claude Code through a local LiteLLM proxy that connects
to a local vLLM backend. This allows Claude Code to use models
running on your local hardware via the Anthropic API format.
USAGE:
local_claude.sh [OPTIONS] [CLAUDE_ARGS]
OPTIONS:
--help Show this help message
--verbose Display environment variables and configuration
--no-check Skip health check of the proxy
--host HOST Override litellm proxy host (default: localhost)
--port PORT Override litellm proxy port (default: 4000)
--token TOKEN Override API token (default: sk-FAKE)
--dry-run Show environment variables and exit without launching
EXAMPLES:
# Launch Claude Code with local proxy
local_claude.sh
# Launch with verbose output
local_claude.sh --verbose
# Launch with custom proxy host
local_claude.sh --host 192.168.1.100 --port 8080
# Show what would be set without launching
local_claude.sh --dry-run
# Pass arguments to Claude Code
local_claude.sh -- /path/to/project
local_claude.sh --verbose -- /path/to/project
SETUP:
1. Start the services:
docker compose -f /home/p2enjoy/jupyterlab/vllm-server/docker-compose.yml up -d
2. Test the connection:
/home/p2enjoy/jupyterlab/vllm-server/launchers/test.sh
3. Add to your PATH (optional):
export PATH="/home/p2enjoy/jupyterlab/vllm-server:$PATH"
4. Then use from anywhere:
local_claude.sh
ENVIRONMENT VARIABLES:
These are set automatically based on vars.env:
- ANTHROPIC_BASE_URL LiteLLM proxy URL
- ANTHROPIC_AUTH_TOKEN API authentication token
- CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC
Disable non-essential network traffic
TROUBLESHOOTING:
If Claude Code doesn't connect:
1. Check if services are running:
docker compose -f /home/p2enjoy/jupyterlab/vllm-server/docker-compose.yml ps
2. Test the proxy manually:
/home/p2enjoy/jupyterlab/vllm-server/launchers/test.sh
3. Check logs:
docker compose -f /home/p2enjoy/jupyterlab/vllm-server/docker-compose.yml logs litellm
4. Try with verbose mode:
local_claude.sh --verbose
EOF
}
ensure_claude_code_installed() {
# Check if claude-code or claude is already installed
if command -v claude-code &> /dev/null || command -v claude &> /dev/null; then
log_success "Claude Code is installed"
return 0
fi
log_warning "Claude Code CLI not found. Installing..."
echo ""
# Check if npm is available
if ! command -v npm &> /dev/null; then
log_error "npm is not installed. Cannot install Claude Code."
log_info "Please install Node.js and npm first, then run this script again."
return 1
fi
# Install Claude Code globally
log_info "Installing @anthropic-ai/claude-code..."
if sudo npm install -g @anthropic-ai/claude-code; then
log_success "Claude Code installed successfully"
echo ""
return 0
else
log_error "Failed to install Claude Code"
log_info "Try installing manually with: sudo npm install -g @anthropic-ai/claude-code"
return 1
fi
}
check_health() {
log_info "Checking LiteLLM proxy at ${PROXY_URL}..."
if ! timeout 5 curl -fsS "${PROXY_URL}/health/liveliness" > /dev/null 2>&1; then
log_error "LiteLLM proxy is not responding at ${PROXY_URL}"
log_info ""
log_info "To start the services, run:"
log_info " cd ${PROJECT_DIR}"
log_info " docker compose up -d"
log_info ""
log_info "To skip this check in the future, use: local_claude.sh --no-check"
return 1
fi
log_success "LiteLLM proxy is healthy"
return 0
}
load_environment() {
# Check if vars.env exists
if [ ! -f "$VARS_FILE" ]; then
log_error "Configuration file not found: $VARS_FILE"
return 1
fi
# Load selected vars from vars.env without eval.
load_selected_vars "$VARS_FILE"
if [ -f "$SECRETS_HELPER" ]; then
# shellcheck disable=SC1090
source "$SECRETS_HELPER"
load_secrets_dir "$SECRETS_DIR" >/dev/null
fi
# Override with environment-specific settings
export ANTHROPIC_BASE_URL="${PROXY_URL}"
export ANTHROPIC_AUTH_TOKEN="${ANTHROPIC_AUTH_TOKEN:-sk-FAKE}"
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
return 0
}
show_environment() {
log_info "Environment Configuration:"
echo " ANTHROPIC_BASE_URL: ${ANTHROPIC_BASE_URL}"
echo " ANTHROPIC_AUTH_TOKEN: ${ANTHROPIC_AUTH_TOKEN:0:10}..."
echo " CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: ${CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC}"
if [ -n "${MODEL:-}" ]; then
echo " MODEL: ${MODEL}"
fi
if [ -n "${HF_TOKEN:-}" ]; then
echo " HF_TOKEN: ${HF_TOKEN:0:10}..."
fi
echo ""
}
###############################################################################
# Parse Command Line Arguments
###############################################################################
CLAUDE_ARGS=()
parse_args=true
while [ $# -gt 0 ]; do
case "$1" in
--help)
SHOW_HELP=true
shift
;;
--verbose)
VERBOSE=true
shift
;;
--no-check)
CHECK_HEALTH=false
shift
;;
--host)
require_arg "--host" "${2:-}"
LITELLM_HOST="$2"
PROXY_URL="http://${LITELLM_HOST}:${LITELLM_PORT}"
shift 2
;;
--port)
require_arg "--port" "${2:-}"
LITELLM_PORT="$2"
PROXY_URL="http://${LITELLM_HOST}:${LITELLM_PORT}"
shift 2
;;
--token)
require_arg "--token" "${2:-}"
ANTHROPIC_AUTH_TOKEN="$2"
shift 2
;;
--dry-run)
DRY_RUN=true
CHECK_HEALTH=false
VERBOSE=true
shift
;;
--)
# Stop parsing options, rest are claude args
shift
CLAUDE_ARGS=("$@")
break
;;
*)
# Any other argument is passed to claude
CLAUDE_ARGS+=("$1")
shift
;;
esac
done
###############################################################################
# Main Execution
###############################################################################
main() {
echo ""
echo -e "${CYAN}╔════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║ Local Claude - LiteLLM Proxy Launcher ║${NC}"
echo -e "${CYAN}╚════════════════════════════════════════════════════════════════╝${NC}"
echo ""
# Show help if requested
if [ "$SHOW_HELP" = true ]; then
print_help
return 0
fi
# Load environment variables
if ! load_environment; then
log_error "Failed to load environment configuration"
return 1
fi
# Update proxy URL if custom host/port was provided
export ANTHROPIC_BASE_URL="${PROXY_URL}"
# Show configuration if verbose
if [ "$VERBOSE" = true ]; then
show_environment
fi
# Check proxy health
if [ "$CHECK_HEALTH" = true ]; then
echo ""
if ! check_health; then
return 1
fi
fi
# Dry run mode - just show environment and exit
if [ "$DRY_RUN" = true ]; then
log_info "Dry run mode - environment variables set but claude-code not launched"
return 0
fi
# Ensure Claude Code is installed
echo ""
if ! ensure_claude_code_installed; then
return 1
fi
# Launch Claude Code with the environment variables
echo ""
log_info "Launching Claude Code..."
log_info "Proxy: ${ANTHROPIC_BASE_URL}"
echo ""
# Determine which command to use
if command -v claude-code &> /dev/null; then
CLAUDE_CMD="claude-code"
elif command -v claude &> /dev/null; then
CLAUDE_CMD="claude"
else
log_error "Claude Code CLI could not be found after installation attempt"
return 1
fi
# Launch Claude Code with collected arguments
exec "$CLAUDE_CMD" "${CLAUDE_ARGS[@]}"
}
# Run main function
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
main "$@"
fi