#!/bin/bash
# vLLM + MultiConnector(MooncakeConnector kv_both, LMCacheConnectorV1).
# Skip if either is missing.
set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/common.sh"

if ! "$PYTHON" -c 'import lmcache' 2>/dev/null; then
    echo "SKIP: lmcache module not importable" >&2
    exit 42
fi

BOOTSTRAP_PORT="${BOOTSTRAP_PORT:-8998}"

# MultiConnector wraps a list of sub-connector configs.
# Reference: vllm/distributed/kv_transfer/kv_connector/factory.py +
# docs/features/disagg_prefill.md
KV_CFG=$(cat <<EOF
{
  "kv_connector": "MultiConnector",
  "kv_role": "kv_both",
  "kv_connector_extra_config": {
    "connectors": [
      {"kv_connector": "MooncakeConnector", "kv_role": "kv_both"},
      {"kv_connector": "LMCacheConnectorV1", "kv_role": "kv_both"}
    ]
  }
}
EOF
)
KV_CFG=$(echo "$KV_CFG" | tr -d '\n')

VLLM_MOONCAKE_BOOTSTRAP_PORT="$BOOTSTRAP_PORT" \
CUDA_VISIBLE_DEVICES="$GPU_ID" \
"$PYTHON" -m vllm.entrypoints.openai.api_server \
    "${COMMON_VLLM_ARGS[@]}" \
    --kv-transfer-config "$KV_CFG" \
    > "$LOG_DIR/vllm_stdout.log" 2> "$LOG_DIR/vllm_stderr.log" &
VLLM_PID=$!
echo "$VLLM_PID" > "$LOG_DIR/.vllm.pid"
echo "vLLM PID=$VLLM_PID"
wait_for_ready 300
