# Deploy Kyutai TTS (1.6B EN/FR streaming, 220 ms claimed latency) to
# irv-ml1.
#
# Builds the image locally from NillPointer/Kyutai-TTS-Server via
# docker buildx git URL context. ~5-8 min cold build (CUDA + torch +
# moshi + Kyutai's Mimi codec deps). First start downloads
# kyutai/tts-1.6b-en_fr (~3-6 GB) into the bind-mounted HF cache.
#
# Usage:
#   scripts/elway irv-ml1 --playbook playbooks/deploy-kyutai-tts.yaml
#
# Note: Kyutai's official deploy is Rust + websockets only. This stack
# uses the NillPointer community wrapper to bridge to OpenAI-compat
# HTTP — adds Python overhead on the request path, so measured TTFB
# will be higher than the bare-Rust 220 ms claim. See README.
#
# Idempotent — every step is creates-/when-gated; rerun is safe.

vars:
  compose_dir: /opt/docker/compose/kyutai-tts
  voices_dir: /worktank/kyutai-tts/voices
  cache_dir: /worktank/kyutai-tts/hf_cache
  host_port: "8198"

steps:
  # ── host-side dirs ──────────────────────────────────────────────────

  - name: Ensure /worktank/kyutai-tts root exists (one-time, sudo)
    shell: mkdir -p /worktank/kyutai-tts
    sudo: true
    creates: /worktank/kyutai-tts

  - name: Chown /worktank/kyutai-tts to lkraven
    shell: chown -R lkraven:lkraven /worktank/kyutai-tts
    sudo: true
    when: "[ \"$(stat -c %U /worktank/kyutai-tts)\" != \"lkraven\" ]"

  - name: Ensure voices dir exists
    shell: mkdir -p {{ voices_dir }}
    creates: "{{ voices_dir }}"

  - name: Ensure HF cache dir exists
    shell: mkdir -p {{ cache_dir }}
    creates: "{{ cache_dir }}"

  - name: Ensure compose dir exists
    shell: mkdir -p {{ compose_dir }}
    creates: "{{ compose_dir }}"

  # ── deploy compose + env ────────────────────────────────────────────

  - name: Upload compose.yaml
    upload:
      src: stacks/kyutai-tts/compose.yaml
      dest: "{{ compose_dir }}/compose.yaml"
      mode: "0644"

  - name: Seed .env from template (only if absent)
    upload:
      src: stacks/kyutai-tts/.env.example
      dest: "{{ compose_dir }}/.env"
      mode: "0644"
    when: "[ ! -f {{ compose_dir }}/.env ]"

  # ── build + bring up ────────────────────────────────────────────────

  - name: docker compose build (~5-8 min first time; cached after)
    shell: |
      set -o pipefail
      cd {{ compose_dir }} && docker compose build 2>&1 \
        | grep -vE '^#[0-9]+ |^ => |^=> |Collecting|Downloading|Requirement|Using cached|Installing collected|Successfully (installed|built)|━'

  - name: docker compose up -d
    shell: cd {{ compose_dir }} && docker compose up -d

  - name: Wait for /health to respond (allow ~10 min for first download + warmup)
    shell: |
      for i in $(seq 1 120); do
        curl -sf -o /dev/null --max-time 3 http://localhost:{{ host_port }}/health && exit 0
        sleep 5
      done
      exit 1
    changed_when: "false"

verify:
  - name: /health returns 200
    shell: curl -sf -o /dev/null http://localhost:{{ host_port }}/health
    changed_when: "false"

  - name: /v1/audio/speech returns a real WAV (POST with text body)
    # NillPointer wrapper exposes ONLY /health and POST /v1/audio/speech
    # (no /v1/models, no /v1/audio/voices). Smoke by POSTing and
    # asserting a RIFF WAV comes back.
    shell: |
      out=$(mktemp --suffix=.wav)
      curl -sf -X POST http://localhost:{{ host_port }}/v1/audio/speech \
        -H 'Content-Type: application/json' \
        -d '{"model":"tts-1.6b-en_fr","input":"Verify."}' \
        -o "$out" --max-time 30
      file -b "$out" | grep -q '^RIFF.*WAVE'
      rm -f "$out"
    changed_when: "false"

  - name: Container is running
    shell: docker inspect kyutai-tts --format '{{.State.Status}}' | grep -q running
    changed_when: "false"
