#!/usr/bin/env bash # shellcheck disable=SC2034 # variables consumed by lib/common.sh # qwclaude — launch Claude Code on Alibaba Cloud Bailian's Qwen models # (Anthropic-compatible endpoint). # # Follows the official Bailian "Claude Code 配置" guide. Three billing plans, # each with its own base URL and model lineup: # - Pay-as-you-go : https://dashscope.aliyuncs.com/apps/anthropic (Beijing) # https://dashscope-intl.aliyuncs.com/apps/anthropic (Singapore) # - Coding Plan : https://coding.dashscope.aliyuncs.com/apps/anthropic # - Token Plan : https://token-plan.cn-beijing.maas.aliyuncs.com/apps/anthropic # # Three model tiers within each plan: # max → qwen3.7-max (flagship, full reasoning) # plus → qwen3.7-plus (balanced, ~1/6 cost of max) # flash → qwen3.6-flash (lightweight, fast/cheap) # # Reads the plan-specific Bailian API key from the environment, then falls back # to ~/.zshrc / ~/.bashrc / ~/.bash_profile / ~/.profile: # - Pay-as-you-go : DASHSCOPE_API_KEY # - Coding Plan : DASHSCOPE_CP_API_KEY # - Token Plan : DASHSCOPE_TP_API_KEY # # Quick start (no install needed): # chmod +x qwclaude # ./qwclaude # # Optional — make it globally available: # sudo mv qwclaude /usr/local/bin/ && sudo chmod +x /usr/local/bin/qwclaude # # Use: # qwclaude # pay-as-you-go, qwen3.7-max (Beijing) # qwclaude plus # pay-as-you-go, qwen3.7-plus (balanced, cheaper) # qwclaude fast # qwen3.6-flash as the main model # qwclaude max # explicit qwen3.7-max # qwclaude intl # pay-as-you-go on the Singapore endpoint # qwclaude coding # Coding Plan (qwen3.7-plus recommended) # qwclaude token # Token Plan team edition (qwen3.7-max) # qwclaude coding fast # combine plan + flash # qwclaude coding plus # Coding Plan, qwen3.7-plus (default) # qwclaude token plus # Token Plan, qwen3.7-plus # qwclaude long # request 1M context window (Qwen native support) # qwclaude effort max # set reasoning effort (low|medium|high|xhigh|max) # qwclaude update # git pull latest from this repo # qwclaude --help # any remaining flag is forwarded to claude # # Optional env overrides (take precedence over the positional aliases): # QWEN_PLAN=payg|coding|token-plan # billing plan # QWEN_REGION=cn|intl # pay-as-you-go region (cn=Beijing) # QWEN_MODEL=qwen3.7-max # main model (any model name) # QWEN_FLASH_MODEL=qwen3.6-flash # flash / haiku / subagent tier # QWEN_BASE_URL=https://.../apps/anthropic # custom Anthropic-compatible base URL # QWEN_CTX=1048576 # max context tokens (defaults to 1M with `long`) # QWEN_OUTPUT=8000 # cap output tokens # QWEN_EFFORT=max # CLAUDE_CODE_EFFORT_LEVEL # # In-session switch: # /model qwen3.6-flash # switch to the flash tier # /model qwen3.7-plus # switch to the plus tier # /model qwen3.7-max # switch to max # set -euo pipefail # Resolve symlink to find the real script directory. _SELF="$0" if [ -L "$_SELF" ]; then _LINK="$(readlink "$_SELF")" case "$_LINK" in /*) _SELF="$_LINK" ;; *) _SELF="$(cd "$(dirname "$_SELF")" && pwd)/$_LINK" ;; esac fi SELF_DIR="$(cd "$(dirname "$_SELF")" && pwd)" if [ -f "$SELF_DIR/lib/common.sh" ]; then source "$SELF_DIR/lib/common.sh" elif [ -f "$SELF_DIR/../lib/common.sh" ]; then source "$SELF_DIR/../lib/common.sh" else echo "qwclaude: cannot find lib/common.sh. Reinstall or cd to repo." >&2; exit 1; fi check_version "qwclaude" "$@" # Handle the update subcommand before the API-key check so it works key-less. if [ $# -gt 0 ] && [ "$1" = "update" ]; then do_update "qwclaude" "QWCLAUDE_HOME" fi PLAN="${QWEN_PLAN:-payg}" REGION="${QWEN_REGION:-cn}" MODEL_TIER="${QWEN_MODEL_TIER:-max}" # max | plus | flash LONG_CTX=0 EFFORT="${QWEN_EFFORT:-}" # Positional aliases: plan, region, model-tier, long, effort. while [ $# -gt 0 ]; do case "$1" in coding) PLAN="coding"; shift ;; token-plan|token|tp) PLAN="token-plan"; shift ;; payg|pay-as-you-go) PLAN="payg"; shift ;; intl|singapore|sg) REGION="intl"; shift ;; cn|beijing) REGION="cn"; shift ;; max|pro) MODEL_TIER="max"; shift ;; plus) MODEL_TIER="plus"; shift ;; fast|flash) MODEL_TIER="flash"; shift ;; long) LONG_CTX=1; shift ;; effort) case "${2:-}" in low|medium|high|xhigh|max) EFFORT="$2"; shift 2 ;; *) echo "qwclaude: invalid effort level '${2:-}'. Use: low medium high xhigh max" >&2; exit 1 ;; esac ;; --) shift; break ;; *) break ;; esac done # Resolve base URL and model lineup from the chosen plan (per Bailian docs). case "$PLAN" in token-plan) BASE_URL_DEFAULT="https://token-plan.cn-beijing.maas.aliyuncs.com/apps/anthropic" PRO_MODEL_DEFAULT="qwen3.7-max" PLUS_MODEL_DEFAULT="qwen3.7-plus" FLASH_MODEL_DEFAULT="qwen3.6-flash" PLAN_LABEL="Token Plan" KEY_VAR="DASHSCOPE_TP_API_KEY" ;; coding) # Coding Plan serves qwen3.7-plus (recommended) and qwen3.6-plus. BASE_URL_DEFAULT="https://coding.dashscope.aliyuncs.com/apps/anthropic" PRO_MODEL_DEFAULT="qwen3.7-plus" PLUS_MODEL_DEFAULT="qwen3.7-plus" FLASH_MODEL_DEFAULT="qwen3.6-plus" PLAN_LABEL="Coding Plan" KEY_VAR="DASHSCOPE_CP_API_KEY" ;; *) PLAN="payg" if [ "$REGION" = "intl" ]; then BASE_URL_DEFAULT="https://dashscope-intl.aliyuncs.com/apps/anthropic" else BASE_URL_DEFAULT="https://dashscope.aliyuncs.com/apps/anthropic" fi PRO_MODEL_DEFAULT="qwen3.7-max" PLUS_MODEL_DEFAULT="qwen3.7-plus" FLASH_MODEL_DEFAULT="qwen3.6-flash" PLAN_LABEL="pay-as-you-go" KEY_VAR="DASHSCOPE_API_KEY" ;; esac # Resolve the plan-specific API key: env var first, then shell rc files. API_KEY="$(resolve_api_key "$KEY_VAR")" if [ -z "$API_KEY" ]; then echo "❌ $KEY_VAR not found (needed for Bailian $PLAN_LABEL)." >&2 echo " Add one line to ~/.zshrc or ~/.bashrc:" >&2 echo " export $KEY_VAR=sk-xxxxxxxxxxxxxxxxxx" >&2 exit 1 fi BASE_URL="${QWEN_BASE_URL:-$BASE_URL_DEFAULT}" PRO_MODEL="${QWEN_MODEL:-$PRO_MODEL_DEFAULT}" PLUS_MODEL="${QWEN_PLUS_MODEL:-$PLUS_MODEL_DEFAULT}" FLASH_MODEL="${QWEN_FLASH_MODEL:-$FLASH_MODEL_DEFAULT}" # Pick the main model from the chosen tier. case "$MODEL_TIER" in plus) MAIN_MODEL="$PLUS_MODEL" ;; flash) MAIN_MODEL="$FLASH_MODEL" ;; *) MAIN_MODEL="$PRO_MODEL" ;; esac # Surface the other model(s) in Claude Code's /model picker for mid-session # switching. With three tiers, pick the one that's most useful to switch to: # - On max → expose plus (save cost) # - On plus → expose max (upgrade to flagship) # - On flash → expose max (biggest capability jump) case "$MAIN_MODEL" in "$PRO_MODEL") OTHER_MODEL="$PLUS_MODEL" OTHER_DESC="Qwen 3.7 Plus — balanced, ~1/6 cost of max" ;; "$PLUS_MODEL") OTHER_MODEL="$PRO_MODEL" OTHER_DESC="Qwen 3.7 Max — full reasoning" ;; *) OTHER_MODEL="$PRO_MODEL" OTHER_DESC="Qwen 3.7 Max — full reasoning" ;; esac export_anthropic_base export API_TIMEOUT_MS="600000" # Subagents run on the cheaper flash tier. export CLAUDE_CODE_SUBAGENT_MODEL="$FLASH_MODEL" set_custom_model "$OTHER_MODEL" "$MAIN_MODEL" "$OTHER_DESC" CTX="${QWEN_CTX:-}" OUTPUT_CAP="${QWEN_OUTPUT:-}" apply_context_and_effort banner="🚀 Claude Code on Qwen (Bailian $PLAN_LABEL) → $MAIN_MODEL ($BASE_URL)" [ -n "$CTX" ] && banner="$banner | ctx=$CTX" [ -n "$EFFORT" ] && banner="$banner | effort=$EFFORT" [ "$OTHER_MODEL" != "$MAIN_MODEL" ] && banner="$banner (switch mid-session via /model)" echo "$banner" exec claude "$@"