diff --git a/console-icon.ico b/console-icon.ico new file mode 100644 index 0000000..98bad0f Binary files /dev/null and b/console-icon.ico differ diff --git a/console-icon.svg b/console-icon.svg new file mode 100644 index 0000000..92ee654 --- /dev/null +++ b/console-icon.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/console.bat b/console.bat new file mode 100644 index 0000000..26d5004 --- /dev/null +++ b/console.bat @@ -0,0 +1,162 @@ +@echo off +chcp 65001 >nul +setlocal EnableExtensions EnableDelayedExpansion +cd /d "%~dp0" +title Writing Agent +set "PS=powershell -NoProfile -ExecutionPolicy Bypass -File" +set "CTL=%~dp0console.ps1" + +call :ensure +if errorlevel 1 ( + echo. + echo 启动失败。 + pause + goto :menu +) +start "" "http://127.0.0.1:23337/" + +:menu +cls +echo. +echo ======================================== +echo Writing Agent 本机 +echo http://localhost:23337 +echo 不走隧道,也不在服务机上跑。 +echo 不监视文件。改完代码后输入 update。 +echo ======================================== +echo. +echo start 启动服务并打开页面 +echo stop 停止服务 +echo restart 重启服务 +echo update 用当前代码重启服务 +echo open 打开页面 +echo status 端口和 git 状态 +echo pull git pull --ff-only +echo push git push +echo log 最近日志 +echo help +echo exit 退出控制台,服务继续运行 +echo. +set "LINE=" +set /p "LINE=指令: " +if not defined LINE goto :menu +call :dispatch "%LINE%" +if "%ERRORLEVEL%"=="2" goto :eof +goto :menu + +:dispatch +set "RAW=%~1" +set "CHK=%RAW:&=%" +if not "%CHK%"=="%RAW%" goto :bad +set "CHK=%RAW:|=%" +if not "%CHK%"=="%RAW%" goto :bad +set "CHK=%RAW:>=%" +if not "%CHK%"=="%RAW%" goto :bad +set "CHK=%RAW:<=%" +if not "%CHK%"=="%RAW%" goto :bad +set "CMD=" +for /f "tokens=1" %%A in ("%RAW%") do set "CMD=%%A" +if /i "%CMD%"=="exit" exit /b 2 +if /i "%CMD%"=="quit" exit /b 2 +if /i "%CMD%"=="help" goto :show +if /i "%CMD%"=="start" goto :do_start +if /i "%CMD%"=="stop" goto :do_stop +if /i "%CMD%"=="restart" goto :do_update +if /i "%CMD%"=="update" goto :do_update +if /i "%CMD%"=="open" goto :do_open +if /i "%CMD%"=="status" goto :do_status +if /i "%CMD%"=="pull" goto :do_pull +if /i "%CMD%"=="push" goto :do_push +if /i "%CMD%"=="log" goto :do_log +goto :bad + +:show +echo. +echo update 重启进程以加载当前代码,不监视文件。 +echo pull 快进拉取,不自动重启。拉取后需要 update。 +echo push 推送当前分支,不提交、不强制推送。 +echo exit 只关这个窗口。 +echo. +pause +exit /b 0 + +:do_start +echo. +%PS% "%CTL%" -Action start +if errorlevel 1 goto :held +start "" "http://127.0.0.1:23337/" +echo. +pause +exit /b 0 + +:do_stop +echo. +%PS% "%CTL%" -Action stop +echo. +pause +exit /b 0 + +:do_update +echo. +%PS% "%CTL%" -Action restart +if errorlevel 1 goto :held +echo 已用当前代码重启,没有开启文件监视。 +echo. +pause +exit /b 0 + +:do_open +start "" "http://127.0.0.1:23337/" +exit /b 0 + +:do_status +echo. +%PS% "%CTL%" -Action status +echo. +pause +exit /b 0 + +:do_pull +echo. +git pull --ff-only +echo. +echo 代码已更新到磁盘后,输入 update 才会重启服务。 +echo. +pause +exit /b 0 + +:do_push +echo. +git push +echo. +pause +exit /b 0 + +:do_log +echo. +%PS% "%CTL%" -Action log +echo. +pause +exit /b 0 + +:ensure +set "MODE=" +for /f "delims=" %%A in ('%PS% "%CTL%" -Action query') do set "MODE=%%A" +if /i "!MODE!"=="web" exit /b 0 +echo. +echo 正在启动服务(不监视文件)... +%PS% "%CTL%" -Action start +if errorlevel 1 exit /b 1 +exit /b 0 + +:bad +echo. +echo 不认识这条指令。输入 help 查看。 +echo. +pause +exit /b 0 + +:held +echo. +pause +exit /b 0 diff --git a/console.ps1 b/console.ps1 new file mode 100644 index 0000000..f71e58b --- /dev/null +++ b/console.ps1 @@ -0,0 +1,166 @@ +# Start and stop the local writing-agent web server. File watch stays off. +param( + [Parameter(Mandatory = $true)] + [ValidateSet("query", "start", "stop", "restart", "status", "log")] + [string]$Action +) + +$ErrorActionPreference = "Stop" +$ProgressPreference = "SilentlyContinue" +$Root = Split-Path -Parent $MyInvocation.MyCommand.Path +$Port = 23337 +$Log = Join-Path $env:USERPROFILE ".writing-agent\logs\runtime.log" + +function Get-ListenerPid { + $lines = netstat -ano -p tcp | Select-String ":$Port\s" | Select-String "LISTENING" + foreach ($line in $lines) { + if ("$line" -match '\sLISTENING\s+(\d+)\s*$') { + return [int]$Matches[1] + } + } + return $null +} + +function Get-ProcessById([int]$ProcessId) { + Get-CimInstance Win32_Process -Filter "ProcessId=$ProcessId" -ErrorAction SilentlyContinue +} + +function Test-ServerCommand([string]$CommandLine) { + if (-not $CommandLine) { return $false } + if ($CommandLine -match 'Cursor|cursor-agent') { return $false } + return $CommandLine -match 'src[\\/]+server[\\/]+web-server|web:watch|tsx(\.cmd)?\s+watch|npm(\.cmd)?\s+run\s+web' +} + +function Get-ChainText([int]$ProcessId) { + $parts = New-Object System.Collections.Generic.List[string] + $current = $ProcessId + for ($i = 0; $i -lt 10; $i++) { + $proc = Get-ProcessById $current + if (-not $proc) { break } + $cmd = [string]$proc.CommandLine + $parts.Add($cmd) + if ($cmd -match 'Cursor|cursor-agent') { break } + $parent = [int]$proc.ParentProcessId + if ($parent -le 0 -or $parent -eq $current) { break } + $current = $parent + } + return ($parts -join "`n") +} + +function Get-ProtectedPids { + $protected = New-Object 'System.Collections.Generic.HashSet[int]' + $current = $PID + for ($i = 0; $i -lt 8; $i++) { + if ($current -le 0 -or -not $protected.Add($current)) { break } + $proc = Get-ProcessById $current + if (-not $proc) { break } + $parent = [int]$proc.ParentProcessId + if ($parent -le 0 -or $parent -eq $current) { break } + $current = $parent + } + return $protected +} + +function Test-ForeignConsole([string]$CommandLine) { + if (-not $CommandLine) { return $false } + return $CommandLine -match 'tunnel\.ps1|ops-menu|srv\.bat|console\.bat' +} + +function Get-StopRoot([int]$ProcessId, $Protected) { + $current = $ProcessId + $root = $ProcessId + $matched = $false + for ($i = 0; $i -lt 10; $i++) { + if ($Protected.Contains($current)) { break } + $proc = Get-ProcessById $current + if (-not $proc) { break } + $cmd = [string]$proc.CommandLine + if ($cmd -match 'Cursor|cursor-agent') { break } + if (Test-ForeignConsole $cmd) { break } + if (Test-ServerCommand $cmd) { + $root = $current + $matched = $true + } + $parent = [int]$proc.ParentProcessId + if ($parent -le 0 -or $parent -eq $current) { break } + $current = $parent + } + if (-not $matched) { return $null } + if ($Protected.Contains($root)) { return $null } + return $root +} + +function Stop-Server { + $listener = Get-ListenerPid + if (-not $listener) { + Write-Output "down" + return + } + $root = Get-StopRoot $listener (Get-ProtectedPids) + if (-not $root) { + throw "Port $Port is in use by another process (pid $listener)." + } + & taskkill.exe /F /T /PID $root | Out-Null + if ($LASTEXITCODE -ne 0) { throw "taskkill failed ($LASTEXITCODE)" } + Start-Sleep -Seconds 1 + if (Get-ListenerPid) { throw "Port $Port is still in use." } + Write-Output "stopped" +} + +function Get-Mode { + $listener = Get-ListenerPid + if (-not $listener) { return "down" } + $chain = Get-ChainText $listener + if ($chain -notmatch 'web-server|npm(\.cmd)?\s+run\s+web') { return "other" } + if ($chain -match 'web:watch|tsx(\.cmd)?\s+watch') { return "watch" } + return "web" +} + +function Start-Server { + $mode = Get-Mode + if ($mode -eq "other") { + throw "Port $Port is in use by another process (pid $(Get-ListenerPid))." + } + if ($mode -eq "watch") { Stop-Server | Out-Null } + if ($mode -eq "web") { + Write-Output "already" + return + } + $npm = "D:\tool\node\npm.cmd" + $found = Get-Command npm.cmd -ErrorAction SilentlyContinue + if ($found) { $npm = $found.Source } + if (-not (Test-Path $npm)) { throw "npm.cmd not found" } + Start-Process -FilePath "cmd.exe" -ArgumentList "/c", "`"$npm`" run web" -WorkingDirectory $Root -WindowStyle Hidden | Out-Null + $deadline = (Get-Date).AddSeconds(40) + do { + Start-Sleep -Seconds 1 + $listener = Get-ListenerPid + if ($listener) { + $chain = Get-ChainText $listener + if ($chain -notmatch 'ssh\.exe') { + Write-Output "started" + return + } + } + } while ((Get-Date) -lt $deadline) + throw "Server did not listen on $Port." +} + +switch ($Action) { + "query" { Write-Output (Get-Mode) } + "start" { Start-Server } + "stop" { Stop-Server } + "restart" { + Stop-Server | Out-Null + Start-Server + } + "status" { + Write-Output ("mode=" + (Get-Mode)) + Push-Location $Root + try { git status -sb } finally { Pop-Location } + } + "log" { + if (-not (Test-Path $Log)) { Write-Output "no log: $Log"; break } + Get-Content -Path $Log -Tail 40 + } +} diff --git a/start.bat b/start.bat index e90679c..0a08d14 100644 --- a/start.bat +++ b/start.bat @@ -41,7 +41,7 @@ if exist ".env" ( ) echo [信息] 启动服务 http://localhost:%PORT% -echo [信息] 改 src 或 skills 后后端会自动重启 +echo [信息] 不监视文件。改完代码后关掉本窗口再启动,或用 console.bat 输入 update echo [信息] 后端日志在本窗口,文件在 %USERPROFILE%\.writing-agent\logs\runtime.log echo [信息] 浏览器按 F12 打开开发者工具 echo [信息] 桌面窗口请用 start-app.bat @@ -50,6 +50,6 @@ echo. start "" "http://localhost:%PORT%" -call npm run web:watch +call npm run web pause