This repository was archived by the owner on Jul 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRUN.cmd
More file actions
382 lines (357 loc) · 14.4 KB
/
Copy pathRUN.cmd
File metadata and controls
382 lines (357 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
@echo off
:: ============================================================
:: stoneChat launcher (RUN.cmd)
:: Pure-CMD, no PowerShell / no wmic dependencies.
:: Verifies the runtime environment before starting PHP.
:: ============================================================
setlocal DisableDelayedExpansion
cd /d "%~dp0"
set "SC_SOURCE_PATH=%CD%"
echo(%SC_SOURCE_PATH%| findstr /C:"!" >nul
if not errorlevel 1 (
echo.
echo [FAIL] Current stoneChat path contains an exclamation mark !.
echo CMD delayed expansion cannot safely run from this path.
echo Move stoneChat to a path without ! and run RUN.cmd again.
echo.
pause
endlocal
exit /b 1
)
set "STUNNEL_PATH=C:\Program Files\stunnel\bin\stunnel.exe"
if exist "%~dp0CONF.ini" (
for /f "usebackq eol=; tokens=1,* delims==" %%a in ("%~dp0CONF.ini") do (
for /f "tokens=* delims= " %%k in ("%%a") do (
if /i "%%k"=="stunnel" (
for /f "tokens=* delims= " %%s in ("%%b") do set "STUNNEL_PATH=%%s"
)
)
)
)
echo(%STUNNEL_PATH%| findstr /C:"!" >nul
if not errorlevel 1 (
echo.
echo [FAIL] Stunnel path contains an exclamation mark !.
echo CMD delayed expansion cannot safely use that path.
echo Install stunnel in a folder without !, or edit CONF.ini.
echo.
pause
endlocal
exit /b 1
)
setlocal EnableDelayedExpansion
echo.
echo ============================================================
echo stoneChat launcher
echo ============================================================
echo.
set "ERR_COUNT=0"
set "PHP_OK=0"
set "PORT_OK=0"
set "CONF_OK=0"
set "STUNNEL_OK=0"
set "CACERT_OK=0"
set "HISTORY_OK=0"
set "WIN_BUILD=0"
set "SC_MODERN=0"
:: ------------------------------------------------------------
:: 1. PHP 5.4 or later in PATH
:: ------------------------------------------------------------
echo [ 1/7] PHP 5.4 or later in PATH...
php -v >nul 2>&1
if errorlevel 1 (
echo [FAIL] PHP not found in PATH.
echo Install PHP 5.4+ and add php.exe to PATH.
echo ^(5.4+ is required for the built-in web server "php -S"^)
echo Download: https://windows.php.net/downloads/releases/archives/php-5.4.45-Win32-VC9-x86.zip
set /a "ERR_COUNT+=1"
) else (
for /f "delims=" %%v in ('php -r "echo PHP_VERSION;" 2^>nul') do set "PHPVER=%%v"
php -r "exit(version_compare(PHP_VERSION, '5.4.0', '>=') ? 0 : 1);" >nul 2>&1
if errorlevel 1 (
echo [FAIL] PHP version too old. Found !PHPVER!, need 5.4 or later.
echo Download: https://windows.php.net/downloads/releases/archives/php-5.4.45-Win32-VC9-x86.zip
set /a "ERR_COUNT+=1"
) else (
echo [ OK ] PHP !PHPVER! found.
set "PHP_OK=1"
)
)
:: ------------------------------------------------------------
:: 2. CONF.ini present and validate_config-clean
:: ------------------------------------------------------------
echo [ 2/7] CONF.ini present and valid...
if not exist "%~dp0CONF.ini" (
if exist "%~dp0CONF_SMP.INI" (
copy /Y "%~dp0CONF_SMP.INI" "%~dp0CONF.ini" >nul
echo [INFO] Created CONF.ini from CONF_SMP.INI template.
)
)
if not exist "%~dp0CONF.ini" (
echo [FAIL] CONF.ini not found, and no CONF_SMP.INI template available.
echo Please restore CONF_SMP.INI or create CONF.ini.
set /a "ERR_COUNT+=1"
) else (
if "!PHP_OK!"=="1" (
for /f "delims=" %%e in ('php -r "require 'Server/config.php';$c=sc_load_config('CONF.ini');$e=sc_config_fatal_errors(sc_validate_config($c));echo empty($e)?'OK':'ERR:'.implode(',',$e);" 2^>nul') do set "VALIDATE_OUT=%%e"
if "!VALIDATE_OUT!"=="OK" (
echo [ OK ] CONF.ini present and valid.
set "CONF_OK=1"
) else (
echo [FAIL] CONF.ini validation failed: !VALIDATE_OUT!
echo Edit CONF.ini ^(server, users, models, API keys^).
set /a "ERR_COUNT+=1"
)
) else (
echo [SKIP] Skipped ^(PHP not available -- fix item 1 first^).
)
)
:: ------------------------------------------------------------
:: 3. Port 9999 (or [server] port) not already bound
:: ------------------------------------------------------------
echo [ 3/7] Listening port availability...
set "SC_PORT=9999"
if "!CONF_OK!"=="1" (
for /f "delims=" %%p in ('php -r "require 'Server/config.php';$c=sc_load_config('CONF.ini');echo isset($c['server']['port'])?(int)$c['server']['port']:9999;" 2^>nul') do set "SC_PORT=%%p"
if "!SC_PORT!"=="" set "SC_PORT=9999"
for /f "delims=" %%s in ('php -r "require 'Server/config.php';$c=sc_load_config('CONF.ini');echo isset($c['paths']['stunnel'])?$c['paths']['stunnel']:'';" 2^>nul') do set "STUNNEL_PATH=%%s"
if "!STUNNEL_PATH!"=="" set "STUNNEL_PATH=C:\Program Files\stunnel\bin\stunnel.exe"
if not exist "!STUNNEL_PATH!" (
if exist "C:\Program Files (x86)\stunnel\bin\stunnel.exe" (
set "STUNNEL_PATH=C:\Program Files (x86)\stunnel\bin\stunnel.exe"
)
)
)
:PORT_CHECK_LOOP
set "CONFLICT_PIDS="
if not exist "%~dp0.tmp" mkdir "%~dp0.tmp"
netstat -ano > "%~dp0.tmp\netstat.tmp"
findstr /C:"LISTENING" "%~dp0.tmp\netstat.tmp" > "%~dp0.tmp\netstat_list.tmp"
findstr /C:":!SC_PORT! " "%~dp0.tmp\netstat_list.tmp" > "%~dp0.tmp\netstat_match.tmp"
for /f "tokens=5" %%P in ('type "%~dp0.tmp\netstat_match.tmp" 2^>nul') do (
echo !CONFLICT_PIDS! | findstr /C:" %%P " >nul
if errorlevel 1 set "CONFLICT_PIDS=!CONFLICT_PIDS! %%P "
)
del /q "%~dp0.tmp\netstat.tmp" "%~dp0.tmp\netstat_list.tmp" "%~dp0.tmp\netstat_match.tmp" 2>nul
if not "!CONFLICT_PIDS!"=="" (
echo [WARN] Port !SC_PORT! is already in use by PID^(s^):!CONFLICT_PIDS!
set "SC_GHOST_PIDS="
for %%K in (!CONFLICT_PIDS!) do (
if not "%%K"=="0" if not "%%K"=="4" (
tasklist /FI "PID eq %%K" /NH 2>nul | findstr /R /C:"[ ][ ]*%%K[ ]" >nul
if errorlevel 1 set "SC_GHOST_PIDS=!SC_GHOST_PIDS! %%K "
)
)
if not "!SC_GHOST_PIDS!"=="" (
echo [WARN] Port !SC_PORT! has stale listener PID^(s^):!SC_GHOST_PIDS!
echo Windows reports the port as busy, but no matching process exists.
call :SC_FIND_FREE_PORT "!SC_PORT!"
if defined SC_ALT_PORT (
echo [INFO] Using fallback port !SC_ALT_PORT! for this run.
set "SC_PORT=!SC_ALT_PORT!"
set "PORT_OK=1"
) else (
echo [FAIL] Port !SC_PORT! is unusable and no fallback port was found.
set /a "ERR_COUNT+=1"
)
goto :AFTER_PORT_CHECK
)
set /p "KILL_CHOICE= Do you want to automatically kill this process? (y/n): "
if /i "!KILL_CHOICE!"=="y" (
set "KILL_FAILED=0"
for %%K in (!CONFLICT_PIDS!) do (
if "%%K"=="0" (
echo [FAIL] PID 0 owns this port. It is a protected system entry.
echo Change [server] port in CONF.ini and run RUN.cmd again.
set "KILL_FAILED=1"
) else if "%%K"=="4" (
echo [FAIL] PID 4 owns this port. It is the Windows System process.
echo Change [server] port in CONF.ini or free the binding as Administrator.
set "KILL_FAILED=1"
) else (
echo [INFO] Attempting to kill PID %%K...
set "TASKKILL_LOG=%~dp0.tmp\taskkill_%%K.tmp"
taskkill /F /T /PID %%K > "!TASKKILL_LOG!" 2>&1
if errorlevel 1 (
echo [FAIL] taskkill failed for PID %%K:
type "!TASKKILL_LOG!"
echo Try running RUN.cmd as Administrator, or edit [server] port.
set "KILL_FAILED=1"
) else (
type "!TASKKILL_LOG!" >nul
echo [ OK ] PID %%K killed.
)
del /q "!TASKKILL_LOG!" 2>nul
)
)
if "!KILL_FAILED!"=="1" (
call :SC_FIND_FREE_PORT "!SC_PORT!"
if defined SC_ALT_PORT (
echo [INFO] Using fallback port !SC_ALT_PORT! for this run.
set "SC_PORT=!SC_ALT_PORT!"
set "PORT_OK=1"
) else (
set /a "ERR_COUNT+=1"
set "PORT_OK=0"
)
goto :AFTER_PORT_CHECK
)
:: Wait a moment for the port to be freed
ping 127.0.0.1 -n 2 >nul
goto :PORT_CHECK_LOOP
) else (
call :SC_FIND_FREE_PORT "!SC_PORT!"
if defined SC_ALT_PORT (
echo [INFO] Port !SC_PORT! is in use and was not killed.
echo [INFO] Using fallback port !SC_ALT_PORT! for this run.
set "SC_PORT=!SC_ALT_PORT!"
set "PORT_OK=1"
) else (
echo [FAIL] Port !SC_PORT! is in use and no fallback port was found.
set /a "ERR_COUNT+=1"
)
)
) else (
echo [ OK ] Port !SC_PORT! is free.
set "PORT_OK=1"
)
:AFTER_PORT_CHECK
:: ------------------------------------------------------------
:: 4. [paths] stunnel executable exists
:: ------------------------------------------------------------
echo [ 4/7] stunnel executable...
if exist "!STUNNEL_PATH!" (
echo [ OK ] stunnel found: "!STUNNEL_PATH!"
set "STUNNEL_OK=1"
) else (
echo [FAIL] stunnel.exe not found at: "!STUNNEL_PATH!"
echo Install stunnel, or edit CONF.ini [paths] stunnel.
echo Download: https://www.stunnel.org/archive/5.x/stunnel-5.26-installer.exe
set /a "ERR_COUNT+=1"
)
:: ------------------------------------------------------------
:: 5. ModernNetwork\cacert.pem present
:: ------------------------------------------------------------
echo [ 5/7] CA certificate ^(cacert.pem^)...
if exist "%~dp0ModernNetwork\cacert.pem" (
echo [ OK ] cacert.pem present.
set "CACERT_OK=1"
) else (
echo [FAIL] ModernNetwork\cacert.pem not found.
echo Run from the stoneChat project root, or restore cacert.pem.
set /a "ERR_COUNT+=1"
)
:: ------------------------------------------------------------
:: 6. HISTORY/ creatable + writable
:: ------------------------------------------------------------
echo [ 6/7] HISTORY directory...
if not exist "%~dp0HISTORY\" (
mkdir "%~dp0HISTORY" >nul 2>&1
)
if exist "%~dp0HISTORY\" (
set "HISTORY_TMP=%~dp0HISTORY\.sc_write_test.tmp"
echo. > "!HISTORY_TMP!" 2>nul
if exist "!HISTORY_TMP!" (
del "!HISTORY_TMP!" >nul 2>&1
echo [ OK ] HISTORY\ exists and is writable.
set "HISTORY_OK=1"
) else (
echo [FAIL] HISTORY\ is not writable.
set /a "ERR_COUNT+=1"
)
) else (
echo [FAIL] Cannot create HISTORY\ at project root.
set /a "ERR_COUNT+=1"
)
:: ------------------------------------------------------------
:: 7. Windows version detection + modern-Windows note
:: ------------------------------------------------------------
echo [ 7/7] Windows version detection...
set "VER_STR="
for /f "tokens=*" %%v in ('ver') do set "VER_STR=%%v"
set "VER_INNER="
for /f "tokens=2 delims=[]" %%a in ("!VER_STR!") do set "VER_INNER=%%a"
set "VER_NUM="
for /f "tokens=2" %%a in ("!VER_INNER!") do set "VER_NUM=%%a"
for /f "tokens=3 delims=." %%a in ("!VER_NUM!") do set "WIN_BUILD=%%a"
if "!WIN_BUILD!"=="" set "WIN_BUILD=0"
set "WIN_BUILD=!WIN_BUILD: =!"
echo(!WIN_BUILD!| findstr /R "^[0-9][0-9]*$" >nul
if errorlevel 1 set "WIN_BUILD=0"
if !WIN_BUILD! GEQ 17763 (
set "SC_MODERN=1"
echo [INFO] Windows build !WIN_BUILD! detected ^(Windows 10 1809 or newer^).
echo Your device looks very modern; many modern tools are available.
echo stoneChat is a retro LLM client ^(IE6 / Windows XP era^).
echo It still runs fine; the UI just looks 20+ years old by design.
echo Expect a brief "Super-Modern-HTML" splash on first page load.
) else (
echo [ OK ] Windows build !WIN_BUILD! ^< 17763; the retro UI will load directly.
)
echo.
if !ERR_COUNT! GTR 0 (
echo ============================================================
echo stoneChat startup check FAILED: !ERR_COUNT! error^(s^).
echo Please fix the issues above and re-run RUN.cmd.
echo ============================================================
echo.
echo Suggested order of steps:
if "!PHP_OK!"=="0" (
echo [Step A] Install PHP 5.4+ and add php.exe to PATH.
echo ^(5.4+ is required for the built-in web server "php -S"^)
echo Download: https://windows.php.net/downloads/releases/archives/php-5.4.45-Win32-VC9-x86.zip
)
if "!STUNNEL_OK!"=="0" (
echo [Step B] Install stunnel and set [paths] stunnel in CONF.ini.
echo Download: https://www.stunnel.org/archive/5.x/stunnel-5.26-installer.exe
)
if "!CONF_OK!"=="0" if "!PHP_OK!"=="1" (
echo [Step C] Edit CONF.ini: set users, models, and API keys.
)
echo.
echo After fixing, re-run RUN.cmd.
echo.
pause
endlocal
endlocal
exit /b 1
)
echo Environment check passed. Launching stoneChat...
echo.
:: ------------------------------------------------------------
:: Launch
:: ------------------------------------------------------------
:: Optional local mock LLM (CONF_SMP MockLocal uses port 9998).
:: Started minimized so a stock sample config can chat out of the box.
if exist "%~dp0Server\api\mock_llm.php" (
echo Starting mock LLM helper on http://localhost:9998/ ...
start "stoneChat-mock" /MIN cmd /c "cd /d ""%~dp0"" && php -S localhost:9998 Pages\router.php"
ping 127.0.0.1 -n 2 >nul
)
if "!SC_MODERN!"=="1" (
echo Opening browser to http://localhost:!SC_PORT!/ ^(-- modern splash first --^)
) else (
echo Opening browser to http://localhost:!SC_PORT!/
)
start "" "http://localhost:!SC_PORT!/"
php -S localhost:!SC_PORT! Pages/router.php
echo.
echo stoneChat has stopped.
pause
endlocal
endlocal
exit /b 0
:SC_FIND_FREE_PORT
set "SC_ALT_PORT="
set "SC_FIND_START=%~1"
echo(!SC_FIND_START!| findstr /R "^[0-9][0-9]*$" >nul
if errorlevel 1 set "SC_FIND_START=9999"
set /a "SC_CANDIDATE=SC_FIND_START+1"
set /a "SC_LAST=SC_FIND_START+20"
for /L %%Q in (!SC_CANDIDATE!,1,!SC_LAST!) do (
if not defined SC_ALT_PORT (
netstat -ano | findstr /C:":%%Q " | findstr /C:"LISTENING" >nul
if errorlevel 1 set "SC_ALT_PORT=%%Q"
)
)
goto :eof