# Non-destructive AstrBot live probe helper. # Usage: # $env:ASTRBOT_PROBE_KEY = "abk_..." # $env:ASTRBOT_PROBE_BASE = "http://host:port" # powershell -NoProfile -ExecutionPolicy Bypass -File .\run-live-probe.ps1 $ErrorActionPreference = "Stop" $base = if ($env:ASTRBOT_PROBE_BASE) { $env:ASTRBOT_PROBE_BASE } else { "http://127.0.0.1:6185" } $key = $env:ASTRBOT_PROBE_KEY if (-not $key) { throw "Set ASTRBOT_PROBE_KEY first" } $outDir = Split-Path -Parent $MyInvocation.MyCommand.Path $sseFile = Join-Path $outDir "astrbot-live-probe-sse.raw.txt" $sumFile = Join-Path $outDir "astrbot-live-probe-summary.json" $logFile = Join-Path $outDir "astrbot-live-probe-run.log" function Log($m) { Add-Content -Path $logFile -Value ("[{0}] {1}" -f (Get-Date -Format o), $m) -Encoding utf8 } function Invoke-SseChat([string]$body, [string]$label, [int]$timeoutSec = 90) { Log "chat $label begin" $req = [System.Net.HttpWebRequest]::Create("$base/api/v1/chat") $req.Method = "POST"; $req.Timeout = $timeoutSec * 1000; $req.ReadWriteTimeout = 20000 $req.ContentType = "application/json; charset=utf-8"; $req.Accept = "text/event-stream, application/json" $req.Headers.Add("Authorization", "Bearer $key") $bytes = [System.Text.Encoding]::UTF8.GetBytes($body) $req.ContentLength = $bytes.Length $rs = $req.GetRequestStream(); $rs.Write($bytes,0,$bytes.Length); $rs.Close() $resp = $req.GetResponse(); $reader = New-Object System.IO.StreamReader($resp.GetResponseStream()) $sb = New-Object System.Text.StringBuilder $deadline = [DateTime]::UtcNow.AddSeconds($timeoutSec) $lastData = [DateTime]::UtcNow; $plain = 0; $saved = $false while ([DateTime]::UtcNow -lt $deadline) { if ($reader.EndOfStream) { break } try { $line = $reader.ReadLine() } catch { break } if ($null -eq $line) { break } [void]$sb.AppendLine($line) if ($line.StartsWith("data:")) { $lastData = [DateTime]::UtcNow if ($line -match '"type": "plain"') { $plain++ } if ($line -match '"type": "message_saved"' -or $line -match '"type": "error"') { $saved = $true } } if ($plain -gt 0 -and (([DateTime]::UtcNow - $lastData).TotalSeconds -ge 8)) { break } if ($saved -and (([DateTime]::UtcNow - $lastData).TotalSeconds -ge 2)) { break } } try { $reader.Close() } catch {}; try { $resp.Close() } catch {} Log "chat $label done len=$($sb.Length) plain=$plain saved=$saved" return [PSCustomObject]@{ Length=$sb.Length; Body=$sb.ToString(); Plain=$plain; Saved=$saved } } function Get-Json($uri) { $req = [System.Net.HttpWebRequest]::Create($uri); $req.Method = "GET"; $req.Timeout = 20000 $req.Headers.Add("Authorization", "Bearer $key") $resp = $req.GetResponse(); $r = New-Object System.IO.StreamReader($resp.GetResponseStream()); $t = $r.ReadToEnd(); $r.Close(); $resp.Close(); return $t } "" | Set-Content $logFile -Encoding utf8 Log "start base=$base" $rA = Invoke-SseChat '{"message":"desktop-probe A","username":"desktop-companion","session_id":"desktop-companion-s1","enable_streaming":true}' "companion" 90 $rB = Invoke-SseChat '{"message":"desktop-probe B","username":"desktop-operations","session_id":"desktop-operations-s1","enable_streaming":true}' "operations" 90 $sessA = Get-Json "$base/api/v1/chat/sessions?username=desktop-companion&page=1&page_size=20" $sessB = Get-Json "$base/api/v1/chat/sessions?username=desktop-operations&page=1&page_size=20" $raw = "=== companion ===`n$($rA.Body)`n=== operations ===`n$($rB.Body)`n=== sessions companion ===`n$sessA`n=== sessions operations ===`n$sessB" ($raw -replace [regex]::Escape($key), "abk_[REDACTED]") | Set-Content $sseFile -Encoding utf8 function Types($b){ $m=@{}; foreach($x in [regex]::Matches($b,'"type"\s*:\s*"([^"]+)"')){ $k=$x.Groups[1].Value; if($m.ContainsKey($k)){$m[$k]++}else{$m[$k]=1}}; $m } $summary = [ordered]@{ ok=$true; base=$base; companion=@{length=$rA.Length;plain=$rA.Plain;saved=$rA.Saved;types=(Types $rA.Body)}; operations=@{length=$rB.Length;plain=$rB.Plain;saved=$rB.Saved;types=(Types $rB.Body)}; sessionsCompanion=($sessA|ConvertFrom-Json); sessionsOperations=($sessB|ConvertFrom-Json) } ($summary | ConvertTo-Json -Depth 8) | Set-Content $sumFile -Encoding utf8 Log "done"