#!/usr/bin/env bash # shellcheck disable=SC2034 # variables consumed by lib/common.sh # kmclaude — launch Claude Code on Moonshot AI Kimi's Anthropic-compatible API. # # Uses the official Kimi Anthropic-compatible endpoint: # https://api.moonshot.cn/anthropic # # Reads KIMI_API_KEY (or MOONSHOT_API_KEY) from the environment first, # then falls back to ~/.zshrc / ~/.bashrc / ~/.bash_profile / ~/.profile. # # Quick start (no install needed): # chmod +x kmclaude # ./kmclaude # # Optional — make it globally available: # sudo mv kmclaude /usr/local/bin/ && sudo chmod +x /usr/local/bin/kmclaude # # Use: # kmclaude # start on kimi-k3 (Kimi flagship) # kmclaude fast # start on kimi-k2.5 (cheaper / faster tier) # kmclaude long # (no-op; 1M context is the default) # kmclaude effort max # set reasoning effort (low|medium|high|xhigh|max) # kmclaude update # git pull latest from this repo # kmclaude --help # any remaining flag is forwarded to claude # # Optional env overrides: # KIMI_MODEL=kimi-k3 # main model # KIMI_FLASH_MODEL=kimi-k2.5 # flash / haiku / subagent tier # KIMI_BASE_URL=https://.../anthropic # custom Anthropic-compatible base URL # KIMI_CTX=1048576 # max context tokens (default: 1M) # KIMI_OUTPUT=8000 # cap output tokens # KIMI_EFFORT=max # CLAUDE_CODE_EFFORT_LEVEL # # In-session switch: # /model kimi-k2.5 # switch to the flash tier # /model kimi-k3 # switch back to pro # 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 "kmclaude: cannot find lib/common.sh. Reinstall or cd to repo." >&2; exit 1; fi check_version "kmclaude" "$@" PRO_MODEL="${KIMI_MODEL:-kimi-k3}" FLASH_MODEL="${KIMI_FLASH_MODEL:-kimi-k2.5}" # Handle the update subcommand before the API-key check so it works key-less. if [ $# -gt 0 ] && [ "$1" = "update" ]; then do_update "kmclaude" "KMCLAUDE_HOME" fi API_KEY="$(resolve_api_key "KIMI_API_KEY")" [ -z "$API_KEY" ] && API_KEY="$(resolve_api_key "MOONSHOT_API_KEY")" if [ -z "$API_KEY" ]; then echo "❌ Neither KIMI_API_KEY nor MOONSHOT_API_KEY found." >&2 echo " Add one line to ~/.zshrc or ~/.bashrc:" >&2 echo " export MOONSHOT_API_KEY=sk-xxxxxxxxxxxxxxxxxx" >&2 echo " Get your key at: https://platform.moonshot.cn/console/api-keys" >&2 exit 1 fi BASE_URL="${KIMI_BASE_URL:-https://api.moonshot.cn/anthropic}" LONG_CTX=0 EFFORT="${KIMI_EFFORT:-}" # Positional alias: `fast`/`flash` runs the flash tier as the main model. MAIN_MODEL="$PRO_MODEL" while [ $# -gt 0 ]; do case "$1" in fast|flash) MAIN_MODEL="$FLASH_MODEL"; shift ;; long) LONG_CTX=1; shift ;; effort) case "${2:-}" in low|medium|high|xhigh|max) EFFORT="$2"; shift 2 ;; *) echo "kmclaude: invalid effort level '${2:-}'. Use: low medium high xhigh max" >&2; exit 1 ;; esac ;; --) shift; break ;; *) break ;; esac done # Surface the other model in Claude Code's /model picker for mid-session switching. if [ "$MAIN_MODEL" = "$PRO_MODEL" ]; then OTHER_MODEL="$FLASH_MODEL" OTHER_DESC="Kimi K2.5 — fast / cheap tier" else OTHER_MODEL="$PRO_MODEL" OTHER_DESC="Kimi K3 — full reasoning" fi export_anthropic_base # Subagents run on the cheaper flash tier. export CLAUDE_CODE_SUBAGENT_MODEL="$FLASH_MODEL" set_custom_model "$OTHER_MODEL" "$MAIN_MODEL" "$OTHER_DESC" CTX="${KIMI_CTX:-1048576}" OUTPUT_CAP="${KIMI_OUTPUT:-}" apply_context_and_effort show_banner_and_launch "Kimi" "$@"