Merge upstream and add generic Codex notifications

This commit is contained in:
2026-08-04 15:16:36 +08:00
31 changed files with 374 additions and 218 deletions
+52 -13
View File
@@ -6,21 +6,60 @@ rem ============================================================
setlocal
set "BIN=%~dp0..\bin"
set "EXE=%BIN%\notify.exe"
set "PART=%BIN%\notify.exe.partial"
set "LOCK=%BIN%\notify.download.lock"
rem first run: kick off a background download, do NOT block the hook
if not exist "%EXE%" call :bootstrap
rem hidden self-reinvocation: detached background downloader (see :downloader)
if "%~1"=="__download" goto downloader
if exist "%EXE%" "%EXE%" %*
endlocal
exit /b
rem common path: exe present -> run directly (keeps piped stdin intact)
if exist "%EXE%" (
"%EXE%" %*
endlocal & exit /b
)
:bootstrap
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 and return immediately.
if not exist "%BIN%" mkdir "%BIN%" 2>nul
rem mkdir is atomic; success = we start the download, failure = already downloading
mkdir "%LOCK%" 2>nul
if errorlevel 1 exit /b
if exist "%EXE%" ( rmdir "%LOCK%" 2>nul & exit /b )
rem launch a detached hidden PowerShell downloader (ShellExecute breaks away from the hook)
powershell -NoProfile -WindowStyle Hidden -Command "Start-Process -WindowStyle Hidden -FilePath powershell -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-File','%~dp0download.ps1','-Url','%DOWNLOAD_URL%','-Out','%EXE%','-Lock','%LOCK%')"
exit /b
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.
rem
rem must use Start-Process (not `start /b`): a child started by cmd inherits the
rem hook's stdout pipe handle, so the caller won't see EOF until the download ends ->
rem the hook would block. Start-Process spawns WITHOUT inheriting handles, so the
rem hook returns immediately while curl keeps running detached.
mkdir "%LOCK%" 2>nul && powershell -nop -w hidden -c "Start-Process -WindowStyle Hidden -FilePath '%~f0' -ArgumentList '__download'" >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 Some hook clients display this progress JSON; others ignore it.
echo {"suppressOutput":true,"systemMessage":"[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