#!/usr/bin/env bash
# Code Claw -> OpenRouter qwen3.6-plus with Kimi CLI fallback
set -euo pipefail

export CLAW_FORCE_OPENAI_COMPAT=1
export OPENAI_API_KEY=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
export OPENAI_BASE_URL="https://openrouter.ai/api/v1"
export CLAW_CONFIG_HOME="${CLAW_CONFIG_HOME:-$HOME/.claw-qwen36-openrouter}"
mkdir -p "$CLAW_CONFIG_HOME"
cd "$HOME/code-claw/rust"

printf 'Code Claw -> OpenRouter qwen/qwen3.6-plus (paid)
'
printf 'CLAW_CONFIG_HOME=%s
' "$CLAW_CONFIG_HOME"
printf 'OPENAI_BASE_URL=%s
' "$OPENAI_BASE_URL"
printf 'Fallback: Kimi CLI (kimi-cli)

'

# Quick probe to check if OpenRouter is available
probe=$(curl -s --max-time 10   "https://openrouter.ai/api/v1/chat/completions"   -H "Authorization: Bearer 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"   -H "Content-Type: application/json"   -d '{"model":"qwen/qwen3.6-plus","max_tokens":5,"messages":[{"role":"user","content":"."}]}') || probe='{"error":{"message":"connection_failed"}}'

if echo "$probe" | grep -q '"code":"429"'; then
  printf 'OpenRouter returned 429 -- falling back to Kimi CLI

'
  exec kimi-cli
fi

# Probe succeeded, launch Code Claw
exec ./target/debug/claw --model 'qwen/qwen3.6-plus'
