-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·329 lines (288 loc) · 10.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·329 lines (288 loc) · 10.9 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
#!/usr/bin/env bash
set -euo pipefail
# claude-ds installer
# Sets up DeepSeek V4 as a backend for Claude Code with Vision MCP support.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CLAUDE_DIR="$HOME/.claude"
CLAUDE_JSON="$HOME/.claude.json"
MCP_INSTALL_DIR="$SCRIPT_DIR/vision-mcp"
SHELL_RC=""
# Clean up any .tmp residue from a previous interrupted run on exit.
# Atomic-write pattern (jq > $f.tmp && mv $f.tmp $f) can leave $f.tmp if jq fails.
_cleanup_tmp() {
rm -f "$CLAUDE_JSON.tmp" \
"$CLAUDE_DIR/claude-ds-vision-mcp.json.tmp" \
"$CLAUDE_DIR/mcp.json.tmp" \
"$CLAUDE_DIR/settings.json.tmp" 2>/dev/null || true
}
trap _cleanup_tmp EXIT
# Colors (disabled if not a terminal)
if [ -t 1 ]; then
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
else
RED=''; GREEN=''; YELLOW=''; BLUE=''; NC=''
fi
info() { echo -e "${BLUE}[info]${NC} $*"; }
ok() { echo -e "${GREEN}[ok]${NC} $*"; }
warn() { echo -e "${YELLOW}[warn]${NC} $*"; }
error() { echo -e "${RED}[error]${NC} $*" >&2; }
# --- Uninstall mode ---
if [[ "${1:-}" == "--uninstall" ]]; then
echo "Uninstalling claude-ds..."
# Remove shell functions
for rc in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.bash_profile"; do
if [ -f "$rc" ] && grep -q "claude-ds.sh" "$rc"; then
# Remove the source line
sed -i.bak '/claude-ds\.sh/d' "$rc"
sed -i.bak '/DEEPSEEK_API_KEY/d' "$rc"
rm -f "$rc.bak"
ok "Removed claude-ds from $rc"
fi
done
# Remove vision MCP config (new location)
VISION_MCP_JSON="$CLAUDE_DIR/claude-ds-vision-mcp.json"
if [ -f "$VISION_MCP_JSON" ]; then
rm -f "$VISION_MCP_JSON"
ok "Removed $VISION_MCP_JSON"
fi
# Clean up legacy: vision in ~/.claude.json (old installs)
if [ -f "$CLAUDE_JSON" ] && jq -e '.mcpServers.vision' "$CLAUDE_JSON" &>/dev/null; then
jq 'del(.mcpServers.vision)' "$CLAUDE_JSON" > "$CLAUDE_JSON.tmp" \
&& mv "$CLAUDE_JSON.tmp" "$CLAUDE_JSON"
ok "Removed vision server from $CLAUDE_JSON (legacy)"
fi
# Clean up legacy: ~/.claude/mcp.json (old installs)
if [ -f "$CLAUDE_DIR/mcp.json" ] && jq -e '.mcpServers.vision' "$CLAUDE_DIR/mcp.json" &>/dev/null; then
jq 'del(.mcpServers.vision)' "$CLAUDE_DIR/mcp.json" > "$CLAUDE_DIR/mcp.json.tmp" \
&& mv "$CLAUDE_DIR/mcp.json.tmp" "$CLAUDE_DIR/mcp.json"
ok "Removed vision server from $CLAUDE_DIR/mcp.json (legacy)"
fi
warn "Manual cleanup needed:"
warn " 1. Remove vision-guard hook from $CLAUDE_DIR/settings.json"
warn " 2. Remove 'mcp__vision' from permissions in $CLAUDE_DIR/settings.json"
echo "Done."
exit 0
fi
# --- Pre-flight checks ---
echo ""
echo "claude-ds installer"
echo "==================="
echo ""
# Check claude CLI
if ! command -v claude &>/dev/null; then
error "Claude Code CLI not found. Install it first:"
error " curl -fsSL https://claude.ai/install.sh | bash"
error " (or: npm install -g @anthropic-ai/claude-code)"
exit 1
fi
ok "Claude Code CLI found: $(which claude)"
# Check jq (needed by vision-guard hook and JSON config edits below)
if ! command -v jq &>/dev/null; then
warn "jq not found. Attempting auto-install..."
if command -v brew &>/dev/null; then
brew install jq
elif command -v apt-get &>/dev/null; then
sudo apt-get install -y jq
elif command -v dnf &>/dev/null; then
sudo dnf install -y jq
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm jq
else
error "Could not auto-install jq. Install it manually:"
error " macOS (no brew): download binary from https://jqlang.github.io/jq/download/"
error " then place it in /usr/local/bin/jq and chmod +x"
error " Linux: apt/dnf/pacman install jq, or download a static binary"
exit 1
fi
fi
ok "jq found"
# Check pngpaste on macOS (optional but recommended)
if [[ "$(uname)" == "Darwin" ]] && ! command -v pngpaste &>/dev/null; then
warn "pngpaste not found (recommended for clipboard support on macOS)"
if command -v brew &>/dev/null; then
read -rp "Install pngpaste via Homebrew? [Y/n] " yn
if [[ "${yn:-Y}" =~ ^[Yy]$ ]]; then
brew install pngpaste
ok "pngpaste installed"
fi
else
warn "Install manually: brew install pngpaste"
fi
fi
# Detect shell config file.
# Priority: zsh > macOS bash (.bash_profile) > Linux bash (.bashrc) > fallback create .zshrc.
# Rationale: on macOS, bash loads .bash_profile (not .bashrc) for login shells,
# which is what Terminal.app opens by default. Linux distros generally use .bashrc.
if [ -f "$HOME/.zshrc" ]; then
SHELL_RC="$HOME/.zshrc"
elif [[ "$(uname)" == "Darwin" ]] && [ -f "$HOME/.bash_profile" ]; then
SHELL_RC="$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
SHELL_RC="$HOME/.bashrc"
else
SHELL_RC="$HOME/.zshrc"
touch "$SHELL_RC"
fi
ok "Shell config: $SHELL_RC"
echo ""
# --- Collect API keys ---
echo "-----------------------------------------"
echo " Step 1: DeepSeek API key (required)"
echo "-----------------------------------------"
echo ""
if [ -z "${DEEPSEEK_API_KEY:-}" ]; then
echo "Get your key at: https://platform.deepseek.com/api_keys"
echo "(input is hidden for security)"
echo ""
read -rsp " DEEPSEEK_API_KEY: " ds_key
echo ""
if [ -z "$ds_key" ]; then
error "DeepSeek API key is required."
exit 1
fi
ok "Key received"
else
ds_key="$DEEPSEEK_API_KEY"
ok "Using DEEPSEEK_API_KEY from environment"
fi
echo ""
echo "-----------------------------------------"
echo " Step 2: Vision API key (optional)"
echo "-----------------------------------------"
echo ""
echo "Enables image analysis for text-only models."
echo "Any OpenAI-compatible vision API works."
echo "Examples: Alibaba Qwen VL, OpenAI GPT-4o, Groq Llama Vision, local Ollama."
echo ""
echo "Press Enter to skip if you don't need vision support."
echo "(input is hidden for security)"
echo ""
read -rsp " VISION_API_KEY: " vision_key
echo ""
vision_base=""
vision_model=""
if [ -n "$vision_key" ]; then
ok "Key received"
echo ""
read -rp " VISION_BASE_URL (e.g., https://api.openai.com/v1): " vision_base
read -rp " VISION_MODEL (e.g., gpt-4o, qwen3-vl-plus): " vision_model
: "${vision_model:=gpt-4o}"
fi
echo ""
# --- Install shell functions ---
info "Installing shell functions..."
# Remove old entries if present
if grep -q "claude-ds.sh\|# claude-ds:" "$SHELL_RC" 2>/dev/null; then
# Remove old block
sed -i.bak '/# claude-ds: DeepSeek/,/^$/d' "$SHELL_RC"
sed -i.bak '/claude-ds\.sh/d' "$SHELL_RC"
sed -i.bak '/DEEPSEEK_API_KEY/d' "$SHELL_RC"
rm -f "$SHELL_RC.bak"
fi
cat >> "$SHELL_RC" << SHELL_BLOCK
# claude-ds: DeepSeek V4 backend for Claude Code
export DEEPSEEK_API_KEY="$ds_key"
source "$SCRIPT_DIR/scripts/claude-ds.sh"
SHELL_BLOCK
ok "Added claude-ds and claude-ds-flash to $SHELL_RC"
# --- Install Vision MCP server ---
if [ -n "$vision_key" ]; then
# Check Python 3.10+ (only needed for Vision MCP)
if ! command -v python3 &>/dev/null; then
error "Python 3 not found. Vision MCP requires Python 3.10 or later."
exit 1
fi
PY_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
PY_MAJOR=$(echo "$PY_VERSION" | cut -d. -f1)
PY_MINOR=$(echo "$PY_VERSION" | cut -d. -f2)
if [ "$PY_MAJOR" -lt 3 ] || { [ "$PY_MAJOR" -eq 3 ] && [ "$PY_MINOR" -lt 10 ]; }; then
error "Python 3.10+ required for Vision MCP (found $PY_VERSION)"
exit 1
fi
ok "Python $PY_VERSION"
info "Installing Vision MCP server..."
# Create venv in repo directory (single source of truth)
if [ ! -d "$MCP_INSTALL_DIR/.venv" ]; then
python3 -m venv "$MCP_INSTALL_DIR/.venv"
fi
# Install dependencies
"$MCP_INSTALL_DIR/.venv/bin/pip" install -q -e "$MCP_INSTALL_DIR"
ok "Vision MCP server installed at $MCP_INSTALL_DIR"
# --- Configure MCP in standalone config (loaded via --mcp-config by claude-ds) ---
PYTHON_PATH="$MCP_INSTALL_DIR/.venv/bin/python"
VISION_MCP_JSON="$CLAUDE_DIR/claude-ds-vision-mcp.json"
info "Configuring Vision MCP in $VISION_MCP_JSON..."
# Atomic write: build to .tmp, then rename. Protects existing config
# from being truncated if jq fails or the process is interrupted.
jq -n --arg py "$PYTHON_PATH" \
--arg key "$vision_key" \
--arg base "$vision_base" \
--arg model "$vision_model" \
'{mcpServers: {vision: {
command: $py,
args: ["-m", "clipboard_vision_mcp.server"],
env: {
VISION_API_KEY: $key,
VISION_BASE_URL: $base,
VISION_MODEL: $model
}
}}}' > "$VISION_MCP_JSON.tmp" \
&& mv "$VISION_MCP_JSON.tmp" "$VISION_MCP_JSON"
ok "Created $VISION_MCP_JSON"
# Clean up legacy: remove vision from ~/.claude.json if present (old installs)
if [ -f "$CLAUDE_JSON" ] && jq -e '.mcpServers.vision' "$CLAUDE_JSON" &>/dev/null; then
jq 'del(.mcpServers.vision)' "$CLAUDE_JSON" > "$CLAUDE_JSON.tmp" \
&& mv "$CLAUDE_JSON.tmp" "$CLAUDE_JSON"
ok "Migrated vision server out of $CLAUDE_JSON"
fi
# --- Configure settings.json ---
info "Configuring settings..."
SETTINGS_JSON="$CLAUDE_DIR/settings.json"
HOOK_PATH="$SCRIPT_DIR/scripts/vision-guard.sh"
chmod +x "$HOOK_PATH"
if [ -f "$SETTINGS_JSON" ]; then
# Add mcp__vision permission if not present
if ! jq -e '.permissions.allow | index("mcp__vision")' "$SETTINGS_JSON" &>/dev/null; then
jq '.permissions.allow += ["mcp__vision"]' "$SETTINGS_JSON" > "$SETTINGS_JSON.tmp" \
&& mv "$SETTINGS_JSON.tmp" "$SETTINGS_JSON"
ok "Added mcp__vision permission"
fi
# Add vision-guard hook if not present
if ! jq -e '.hooks.PreToolUse[] | select(.matcher == "Read") | .hooks[] | select(.command | contains("vision-guard"))' "$SETTINGS_JSON" &>/dev/null; then
jq --arg cmd "$HOOK_PATH" \
'.hooks.PreToolUse = [
{matcher: "Read", hooks: [{type: "command", command: $cmd, timeout: 5}]}
] + .hooks.PreToolUse' "$SETTINGS_JSON" > "$SETTINGS_JSON.tmp" \
&& mv "$SETTINGS_JSON.tmp" "$SETTINGS_JSON"
ok "Added vision-guard hook"
fi
else
# Create minimal settings.json for fresh installs (atomic write)
jq -n --arg cmd "$HOOK_PATH" \
'{
permissions: {allow: ["mcp__vision"]},
hooks: {PreToolUse: [{matcher: "Read", hooks: [{type: "command", command: $cmd, timeout: 5}]}]}
}' > "$SETTINGS_JSON.tmp" \
&& mv "$SETTINGS_JSON.tmp" "$SETTINGS_JSON"
ok "Created $SETTINGS_JSON with vision-guard hook and permissions"
fi
ok "Vision support fully configured"
fi
# --- Done ---
echo ""
echo "========================================="
echo ""
ok "Installation complete!"
echo ""
echo " Restart your shell or run:"
echo " source $SHELL_RC"
echo ""
echo " Then use:"
echo " claude-ds # V4-Pro mode (complex tasks)"
echo " claude-ds-flash # V4-Flash mode (quick tasks)"
echo ""
if [ -n "$vision_key" ]; then
echo " Vision support is enabled."
echo " The model can use see_image / see_clipboard to analyze images."
echo ""
fi