-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-send-function.ps1
More file actions
76 lines (60 loc) · 2.42 KB
/
Copy pathextract-send-function.ps1
File metadata and controls
76 lines (60 loc) · 2.42 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
<#
extract-send-function.ps1
Extracts the FULL body of send() from app.js using brace counting, so we
get the complete function including the fetch('/chat', ...) call and the
SSE stream-reading loop, not just a length-truncated snippet.
Usage:
.\extract-send-function.ps1
.\extract-send-function.ps1 -AliceUrl "http://127.0.0.1:8000"
#>
param(
[string]$AliceUrl = "http://127.0.0.1:8000",
[string]$FunctionName = "send"
)
function Write-Section($title) {
Write-Host ""
Write-Host "=== $title ===" -ForegroundColor Cyan
}
Write-Section "Fetching app.js"
try {
$resp = Invoke-WebRequest -Uri "$AliceUrl/static/app.js" -UseBasicParsing -TimeoutSec 10
$js = $resp.Content
} catch {
Write-Host "[FAIL] Could not fetch app.js -> $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
# Save full snapshot too, in case we need more context later
$snapshotPath = ".\app.js.snapshot"
$js | Out-File -FilePath $snapshotPath -Encoding utf8
Write-Host "Full file saved to $snapshotPath" -ForegroundColor Gray
Write-Section "Extracting function $FunctionName() with brace counting"
$startPattern = "function\s+$FunctionName\s*\([^)]*\)\s*\{"
$m = [regex]::Match($js, $startPattern)
if (-not $m.Success) {
Write-Host "[FAIL] Could not find 'function $FunctionName(' in app.js" -ForegroundColor Red
exit 1
}
$startIdx = $m.Index
$braceIdx = $m.Index + $m.Length - 1 # index of the opening {
$depth = 0
$endIdx = -1
for ($i = $braceIdx; $i -lt $js.Length; $i++) {
$ch = $js[$i]
if ($ch -eq '{') { $depth++ }
elseif ($ch -eq '}') {
$depth--
if ($depth -eq 0) { $endIdx = $i; break }
}
}
if ($endIdx -eq -1) {
Write-Host "[WARN] Braces never balanced, function may not have been fully captured." -ForegroundColor Yellow
$endIdx = [Math]::Min($startIdx + 4000, $js.Length - 1)
}
$fullFunction = $js.Substring($startIdx, $endIdx - $startIdx + 1)
Write-Host $fullFunction -ForegroundColor Gray
$outPath = ".\$FunctionName.function.js"
$fullFunction | Out-File -FilePath $outPath -Encoding utf8
Write-Section "Summary"
Write-Host "Full function saved to $outPath ($($fullFunction.Length) chars, $(($fullFunction -split "`n").Count) lines)" -ForegroundColor Gray
Write-Host "Look for the fetch('/chat', ...) call and how the SSE response body is read." -ForegroundColor Gray
Write-Host "Check specifically whether 'retry' is checked anywhere after the stream finishes." -ForegroundColor Gray