@echo off
title WeChat Agent

set PROJECT_DIR=D:\F\NewI\opencode\daily-workspace\projects\wechat-hermes-gateway
set TOOLS_DIR=%PROJECT_DIR%\tools
set PYTHONW=C:\Users\hmo\AppData\Local\Programs\Python\Python310\pythonw.exe
set INJECTOR=%TOOLS_DIR%\Injector_x64.exe
set DLL=%TOOLS_DIR%\wxhelper_official_39581.dll
set LOG=%PROJECT_DIR%\logs\startup.log

echo [1/4] Waiting for WeChat...
:wait_wechat
tasklist /fi "imagename eq WeChat.exe" 2>nul | find /i "WeChat.exe" >nul
if errorlevel 1 (
    timeout /t 2 /nobreak >nul
    goto wait_wechat
)
echo [2/4] WeChat started, checking wxhelper...

curl -s -m 3 -X POST http://127.0.0.1:19088/api/checkLogin -H "Content-Type: application/json" -d "{}" 2>nul | find "code" >nul
if not errorlevel 1 (
    echo [3/4] wxhelper OK, skipping inject
    goto start_agent
)

echo [3/4] Injecting wxhelper...
%INJECTOR% -n WeChat.exe -i "%DLL%" >> "%LOG%" 2>&1

echo [3/4] Waiting for wxhelper HTTP...
:wait_wxhelper
timeout /t 2 /nobreak >nul
curl -s -m 3 -X POST http://127.0.0.1:19088/api/checkLogin -H "Content-Type: application/json" -d "{}" 2>nul | find "code" >nul
if errorlevel 1 goto wait_wxhelper

:start_agent
echo [4/4] Clearing cache and starting agent...
if exist "%PROJECT_DIR%\scripts\__pycache__" rmdir /s /q "%PROJECT_DIR%\scripts\__pycache__"
start "" "%PYTHONW%" "%PROJECT_DIR%\scripts\wechat_agent.py"
echo Done.
