Files
notify/scripts/notify.cmd
T
chuan 5fad621130 fix: 通知程序改异步后台下载,hook 不再因下载慢阻塞
- exe 缺失时派生脱离 hook 的后台下载进程,立即返回并通过 systemMessage 汇报进度,不受 30s 超时影响
- curl -C - 断点续传,失败保留 .partial 下次接着下
- 原子 mkdir 锁防止多次触发重复下载;进程被硬杀的陈旧锁靠 .partial 闲置自愈
- systemMessage 用 ASCII(中文经 cmd 会被破坏成非法 JSON)
2026-06-24 01:18:41 +08:00

62 lines
2.7 KiB
Batchfile

@echo off
rem ============================================================
rem Download URL (notify.exe is fetched from here on first run) -- edit as needed
set "DOWNLOAD_URL=https://git.pchuan.top/cc-tools/notify/releases/download/v1.0.0/notify.exe"
rem ============================================================
setlocal
set "BIN=%~dp0..\bin"
set "EXE=%BIN%\notify.exe"
set "PART=%BIN%\notify.exe.partial"
set "LOCK=%BIN%\notify.download.lock"
rem hidden self-reinvocation: detached background downloader (see :downloader)
if "%~1"=="__download" goto downloader
rem common path: exe present -> run directly (keeps piped stdin intact)
if exist "%EXE%" (
"%EXE%" %*
endlocal & exit /b
)
rem ---- exe missing: never block the hook ----
rem kick off the download once (atomic mkdir lock); a detached worker survives the
rem hook timeout. then report progress to Claude and return immediately.
if not exist "%BIN%" mkdir "%BIN%" 2>nul
rem self-heal: if a previous worker was hard-killed (shutdown/crash) it may leave a
rem stale lock that blocks all retries. reclaim it so -C - can resume the .partial.
if exist "%LOCK%" call :reclaim
rem atomic lock: only the first hook spawns the worker; others fall through and just
rem report progress -> no duplicate downloads even when hooks fire concurrently.
mkdir "%LOCK%" 2>nul && start "" /b "%~f0" __download <nul >nul 2>&1
rem downloaded size so far (from the .partial file), shown as X.X MB
set "DLBYTES=0"
if exist "%PART%" for %%A in ("%PART%") do set "DLBYTES=%%~zA"
set /a DLKB=DLBYTES/1024
set /a DLMB=DLKB/1024
set /a DLF=(DLKB*10/1024)%%10
rem exit 0 so Claude parses this JSON; systemMessage is shown to the user, the
rem notification itself is skipped this time (suppressOutput hides raw stdout)
echo {"suppressOutput":true,"systemMessage":"[Claude Code Notify] notifier not ready, downloading in background: %DLMB%.%DLF% MB / ~14 MB done. Works automatically once finished; this notification is skipped."}
endlocal & exit /b 0
:downloader
rem detached worker: resume-capable download (-C -), atomic install, always free lock.
rem if curl fails (slow/flaky net), the .partial is kept and the next hook resumes it.
curl -fsSL -C - "%DOWNLOAD_URL%" -o "%PART%"
if not errorlevel 1 move /y "%PART%" "%EXE%" >nul 2>&1
rmdir "%LOCK%" 2>nul
endlocal & exit /b
:reclaim
rem no .partial yet => worker died before downloading anything; reclaim immediately
if not exist "%PART%" ( rmdir "%LOCK%" 2>nul & goto :eof )
rem .partial idle for >120s => worker is dead (a live curl writes continuously); reclaim
for /f "delims=" %%T in ('powershell -nop -c "[int]((Get-Date)-(Get-Item '%PART%').LastWriteTime).TotalSeconds" 2^>nul') do set "IDLE=%%T"
if not defined IDLE goto :eof
if %IDLE% geq 120 rmdir "%LOCK%" 2>nul
goto :eof