sin601/claude-code
0
1#!/bin/bash2set -e3 4export CONFIG_DIR="${CONFIG_DIR:-/config}"5export WORK_DIR="${WORK_DIR:-/workspace}"6export HOME="${HOME:-/root}"7 8echo "========================================"9echo " Claude Code HF Space"10echo " Starting..."11echo "========================================"12 13# 创建必要目录14mkdir -p "${CONFIG_DIR}" "${WORK_DIR}" "${HOME}/.claude" "${HOME}/.cli-proxy-api"15 16# 从 HF Secrets 加载配置17if [ -f /app/scripts/load-secrets.sh ]; then18 /app/scripts/load-secrets.sh19fi20 21# 初始化配置22if [ ! -f "${CONFIG_DIR}/initialized" ]; then23 echo "[Init] First run - initializing configuration..."24 /app/scripts/init-config.sh25 touch "${CONFIG_DIR}/initialized"26fi27 28# 加载用户配置29if [ -f "${CONFIG_DIR}/config.env" ]; then30 echo "[Init] Loading user configuration..."31 set -a32 source "${CONFIG_DIR}/config.env"33 set +a34fi35 36# 设置 Git 凭据37if [ -n "${GIT_TOKEN}" ] && [ -n "${GIT_REPO_URL}" ]; then38 echo "[Init] Configuring Git credentials..."39 if [[ "${GIT_REPO_URL}" == https://github.com/* ]]; then40 echo "https://${GIT_TOKEN}@github.com" > "${HOME}/.git-credentials"41 elif [[ "${GIT_REPO_URL}" == https://huggingface.co/* ]]; then42 echo "https://user:${GIT_TOKEN}@huggingface.co" > "${HOME}/.git-credentials"43 fi44 chmod 600 "${HOME}/.git-credentials"45fi46 47# 启动 CLIProxyAPI48if [ "${ENABLE_PROXY}" = "true" ]; then49 echo "[Service] Starting CLIProxyAPI..."50 /app/scripts/start-proxy.sh &51 sleep 252 export ANTHROPIC_BASE_URL="http://localhost:${PROXY_PORT:-8080}"53fi54 55# 启动 Git 同步56if [ -n "${GIT_REPO_URL}" ] && [ -n "${GIT_TOKEN}" ]; then57 echo "[Service] Starting Git sync..."58 /app/scripts/git-sync.sh init || true59 /app/scripts/git-sync.sh &60fi61 62# 打印状态63echo ""64echo "========================================"65echo " Services Started"66echo "========================================"67echo " Web Terminal: http://localhost:7860"68[ "${ENABLE_PROXY}" = "true" ] && echo " API Proxy: http://localhost:${PROXY_PORT:-8080}"69echo ""70 71# 进入工作目录72cd "${WORK_DIR}"73 74# 启动 Web Terminal75exec ttyd --port 7860 --writable --allow-remote-connections \76 --client-option '{"fontSize": 14, "fontFamily": "Monaco, Menlo, monospace"}' \77 /bin/bash -c "source ~/.bashrc; exec /bin/bash --login"78 