
@echo off
setlocal EnableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
if "%SCRIPT_DIR:~-1%"=="\" set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%"
set "PI_CLI_DIST=%SCRIPT_DIR%\packages\coding-agent\dist\cli.js"

if not defined PI_CODING_AGENT_DIR (
    set "PI_CODING_AGENT_DIR=%SCRIPT_DIR%\.pi\agent"
)

set "NO_ENV=false"
set "ARGS="

:loop
if "%~1"=="" goto endloop
if "%~1"=="--no-env" (
    set "NO_ENV=true"
) else (
    if "%ARGS%"=="" (
        set "ARGS=%1"
    ) else (
        set "ARGS=%ARGS% %1"
    )
)
shift
goto loop
:endloop

if "%NO_ENV%"=="true" (
    set "ANTHROPIC_API_KEY="
    set "ANTHROPIC_OAUTH_TOKEN="
    set "OPENAI_API_KEY="
    set "GEMINI_API_KEY="
    set "GROQ_API_KEY="
    set "CEREBRAS_API_KEY="
    set "XAI_API_KEY="
    set "OPENROUTER_API_KEY="
    set "ZAI_API_KEY="
    set "MISTRAL_API_KEY="
    set "MINIMAX_API_KEY="
    set "MINIMAX_CN_API_KEY="
    set "AI_GATEWAY_API_KEY="
    set "OPENCODE_API_KEY="
    set "COPILOT_GITHUB_TOKEN="
    set "GH_TOKEN="
    set "GITHUB_TOKEN="
    set "HF_TOKEN="
    set "GOOGLE_APPLICATION_CREDENTIALS="
    set "GOOGLE_CLOUD_PROJECT="
    set "GCLOUD_PROJECT="
    set "GOOGLE_CLOUD_LOCATION="
    set "AWS_PROFILE="
    set "AWS_ACCESS_KEY_ID="
    set "AWS_SECRET_ACCESS_KEY="
    set "AWS_SESSION_TOKEN="
    set "AWS_REGION="
    set "AWS_DEFAULT_REGION="
    set "AWS_BEARER_TOKEN_BEDROCK="
    set "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI="
    set "AWS_CONTAINER_CREDENTIALS_FULL_URI="
    set "AWS_WEB_IDENTITY_TOKEN_FILE="
    set "AZURE_OPENAI_API_KEY="
    set "AZURE_OPENAI_BASE_URL="
    set "AZURE_OPENAI_RESOURCE_NAME="
    echo Running without API keys...
)

if not exist "%PI_CLI_DIST%" (
    echo Build output not found: %PI_CLI_DIST% >&2
    echo Run from repo root: npm run build >&2
    exit /b 1
)

node "%PI_CLI_DIST%" %ARGS%

