Repository navigation
Expand file tree
/
Copy pathstart.ps1
More file actions
1384 lines (1292 loc) · 63.3 KB
/
Copy pathstart.ps1
File metadata and controls
1384 lines (1292 loc) · 63.3 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[CmdletBinding()]
param(
[ValidateSet('start', 'stop', 'status', 'trust', 'link', 'phone', 'bridge', 'model')]
[string]$Action = 'start',
[switch]$NoBrowser,
[switch]$IgnoreSecurity,
# phone only: JunOS.exe already asked about turning LAN mode on
[switch]$Yes
)
$ErrorActionPreference = 'Stop'
# windows PowerShell can't write or delete through an 8.3 path
# (the DOS alias, C:\Users\USURIO~2). TEMP is often one
function Add-JunPathApi {
if ('Jun.PathApi' -as [type]) { return }
Add-Type -Namespace Jun -Name PathApi -MemberDefinition @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern uint GetLongPathNameW(string path, System.Text.StringBuilder result, uint size);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern uint GetShortPathNameW(string path, System.Text.StringBuilder result, uint size);
'@
}
function ConvertTo-LongPath([string]$path) {
if ($path -notmatch '~' -or -not [IO.Path]::IsPathRooted($path)) { return $path }
try {
Add-JunPathApi
$head = [IO.Path]::GetFullPath($path)
$tail = @()
while ($head -and -not ([IO.Directory]::Exists($head) -or [IO.File]::Exists($head))) {
$tail = @([IO.Path]::GetFileName($head)) + $tail
$head = [IO.Path]::GetDirectoryName($head)
}
if (-not $head) { return $path }
$buf = [Text.StringBuilder]::new(32767)
if ([Jun.PathApi]::GetLongPathNameW($head, $buf, $buf.Capacity) -eq 0) { return $path }
return [IO.Path]::Combine([string[]](@($buf.ToString()) + $tail))
} catch {
return $path
}
}
function ConvertTo-ShortPath([string]$path) {
try {
Add-JunPathApi
$buf = [Text.StringBuilder]::new(32767)
if ([Jun.PathApi]::GetShortPathNameW($path, $buf, $buf.Capacity) -eq 0) { return $path }
return $buf.ToString()
} catch {
return $path
}
}
$env:TEMP = ConvertTo-LongPath $env:TEMP
$env:TMP = ConvertTo-LongPath $env:TMP
$JunRoot = ConvertTo-LongPath $PSScriptRoot
Set-Location -LiteralPath $JunRoot
try {
[Console]::OutputEncoding = [Text.Encoding]::UTF8
$OutputEncoding = [Text.Encoding]::UTF8
} catch {}
$VTSupported = $false
if ($Host.UI.SupportsVirtualTerminal -or $env:WT_SESSION -or
$env:TERM_PROGRAM -eq 'vscode' -or $PSVersionTable.PSVersion.Major -ge 7) {
$VTSupported = $true
}
if ($VTSupported) {
$e = [char]27
$R = "$e[0m"
$B = "$e[1m"
$D = "$e[2m"
$ACCENT = "$e[38;2;96;205;255m" # #60CDFF Fluent light blue
$BLUE = "$e[38;2;0;120;212m" # #0078D4 Windows system blue
$OK = "$e[38;2;108;203;95m" # #6CCB5F Fluent green
$DANGER = "$e[38;2;255;76;76m" # #FF4C4C Fluent red
$WARN = "$e[38;2;252;225;0m" # #FCE100 Fluent amber
$DIM = "$e[38;2;158;158;158m" # #9E9E9E Neutral gray
$MUTED = "$e[38;2;204;204;204m" # #CCCCCC Light gray
} else {
$R = ''; $B = ''; $D = ''; $ACCENT = ''; $BLUE = ''; $OK = ''
$DANGER = ''; $WARN = ''; $DIM = ''; $MUTED = ''
}
function Step([string]$msg) { Write-Host " ${ACCENT}▸${R} ${B}${msg}${R}" }
function Ok([string]$msg) { Write-Host " ${OK}✓${R} ${MUTED}${msg}${R}" }
function Note([string]$msg) { Write-Host " ${DIM}ℹ ${msg}${R}" }
function Warn_([string]$msg) { Write-Host " ${WARN}⚠${R} ${WARN}${msg}${R}" }
function Fail_([string]$msg) { Write-Host " ${DANGER}✗${R} ${DANGER}${msg}${R}" }
# UUIDs, NOT indices. nvidia-smi enumerates by PCI bus order
# while CUDA sorts by speed, so the same index means different
# cards to the two of them. great.
function Get-GpuOrder {
if (-not (Get-Command nvidia-smi -ErrorAction SilentlyContinue)) { return $null }
$rows = & nvidia-smi --query-gpu=memory.total,uuid --format=csv,noheader,nounits 2>$null
$uuids = @($rows | Where-Object { $_ -match ',' } | ForEach-Object {
$f = $_ -split ','
[pscustomobject]@{ Mb = [int]($f[0].Trim()); Uuid = $f[1].Trim() }
} | Sort-Object Mb -Descending | ForEach-Object { $_.Uuid })
if ($uuids.Count -eq 0) { return $null }
return ($uuids -join ',')
}
# the card the MTP tune was measured on, as one string. MTP is
# multi-token prediction, a small drafter model guesses the next
# few tokens (how many = the draft depth) and the chat model
# checks them all in one pass. string is the vendor, then every
# GPU's name and how much VRAM it has. sorted biggest card first,
# so shuffling cards between slots isn't a change. only a real
# swap is.
#
# keep this in step with the copy in mtp-autotune.ps1. we compare
# what it prints against MTP_TUNED_GPU, so the day the two print a
# different string for the same card, every boot reads as a GPU
# change and re-runs the whole sweep. forever.
#
# there's no rocm-smi branch on windows bare metal, so an AMD box
# gets '' here. no stamp, and no automatic re-tune either.
function Get-GpuSignature {
if (-not (Get-Command nvidia-smi -ErrorAction SilentlyContinue)) { return '' }
$rows = @(& nvidia-smi --query-gpu=name,memory.total --format=csv,noheader,nounits 2>$null |
ForEach-Object { $_.Trim() } | Where-Object { $_ -match ',' })
if ($rows.Count -eq 0) { return '' }
$cards = @($rows | ForEach-Object {
$f = $_ -split ',', 2
[pscustomobject]@{ Mb = [int]($f[1].Trim()); Text = ($f[0].Trim() + ':' + $f[1].Trim()) }
} | Sort-Object Mb -Descending | ForEach-Object { $_.Text })
return 'nvidia:' + ($cards -join ',')
}
# php can't see the card, OMEGA_GPU_VRAM_MB is the only way
# default_num_ctx() hears its size. same probe as install.ps1,
# keep Get-AmdMemoryMb byte for byte the same as the copies there
# and in installer-gui.ps1.
function Get-AmdMemoryMb([object[]]$controllers) {
$entries = @(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Video\*\000*' -ErrorAction SilentlyContinue)
$capMb = 4095
return @($controllers | ForEach-Object {
$card = $_
$pci = if ([string]$card.PNPDeviceID -match '(?i)^PCI\\VEN_[0-9A-F]{4}&DEV_[0-9A-F]{4}') { $Matches[0] } else { '' }
$keys = @($entries | Where-Object {
($pci -and ([string]$_.MatchingDeviceId) -like "$pci*") -or
([string]$_.DriverDesc -eq [string]$card.Name)
})
$mb = @($keys | ForEach-Object {
$memory = $_.'HardwareInformation.qwMemorySize'
if ($memory -is [byte[]] -and $memory.Length -ge 8) {
[BitConverter]::ToUInt64($memory, 0)
} elseif ($null -ne $memory -and $memory -isnot [byte[]]) {
[uint64]$memory
}
} | Where-Object { $_ -gt 0 } | ForEach-Object { [int]($_ / 1MB) } |
Sort-Object -Descending | Select-Object -First 1)
if ($mb.Count -eq 0 -and $card.AdapterRAM -gt 0) {
$wmiMb = [int]([uint64]$card.AdapterRAM / 1MB)
if ($wmiMb -lt $capMb) { $mb = @($wmiMb) }
}
$mb
} | Where-Object { $_ -ge 1024 })
}
function Get-GpuMemoryMb {
if (Get-Command nvidia-smi -ErrorAction SilentlyContinue) {
$out = & nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits 2>$null
$mb = @($out | Where-Object { $_ -match '\d' } | ForEach-Object { [int]($_.Trim()) })
if ($mb.Count -gt 0) { return $mb }
}
try {
$amd = @(Get-CimInstance Win32_VideoController -ErrorAction Stop |
Where-Object { $_.Name -match '(?i)AMD|Radeon' })
return @(Get-AmdMemoryMb $amd)
} catch {
return @()
}
}
function Get-VramMb {
$mb = @(Get-GpuMemoryMb)
if ($mb.Count -eq 0) { return $null }
return [int](($mb | Measure-Object -Maximum).Maximum)
}
$Runtime = Join-Path $JunRoot 'runtime'
$LogDir = Join-Path $Runtime 'logs'
$PidFile = Join-Path $Runtime 'pids.json'
$StateDir = Join-Path $Runtime 'state'
$CaddyDir = Join-Path $Runtime 'caddy'
$CertDir = Join-Path $CaddyDir 'tls'
$Services = 'caddy', 'php', 'memory', 'prewarm', 'tts', 'ollama', 'llamacpp'
# read KEY=VALUE pairs in as env vars, but ONLY when they aren't
# set already, so you can still override one for a single run.
if (Test-Path .env) {
foreach ($line in Get-Content .env) {
# -cmatch ON PURPOSE. turkish 5.1's -match lowercases I to
# dotless i (U+0131), [A-Za-z] misses it, AI_PROVIDER gone
if ($line -cmatch '^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$') {
$k = $Matches[1]; $v = $Matches[2].Trim()
# compose drops the quotes, so do we. else KEY="x" is 3 chars
if ($v -match '^(["''])(.*)\1$') { $v = $Matches[2] }
# the docker hostnames in .env.example mean nothing on bare
# metal, so skip them and use our own localhost defaults.
if ($v -match '://(ollama|tts|kokoro|karaoke|nginx|php|llamacpp)\b') { continue }
if (-not (Get-Item "env:$k" -ErrorAction SilentlyContinue)) {
Set-Item "env:$k" $v
}
}
}
# the header php shows the tts sidecar. install.ps1 writes
# one, but a .env from before that has no line at all, and
# the sidecar survives across runs so a per-run value would
# lock php out of it on the second start. same rule as
# start.sh, only when the line is MISSING.
if (-not (Get-Content .env | Where-Object { $_ -match '^SIDECAR_SECRET=' })) {
$bytes = [byte[]]::new(32)
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
$env:SIDECAR_SECRET = ($bytes | ForEach-Object { $_.ToString('x2') }) -join ''
Add-Content .env "SIDECAR_SECRET=$env:SIDECAR_SECRET"
}
}
$SidecarSecret = if ($env:SIDECAR_SECRET) { $env:SIDECAR_SECRET } else { '' }
# her brain on another machine, mostly colab.ipynb in inference
# only mode. the quick tunnel URL and the gate key are new every
# notebook run, so a link that stopped answering is how we find
# out it changed. ask for Step 3's lines instead of making people
# hand edit .env. only these names get in, the rest of a paste is
# dropped. the manager window can't answer a prompt, it asks with
# its own box before it runs us and sets JUN_LINK_CHECKED.
$CloudKeys = @('AI_PROVIDER', 'OLLAMA_URL', 'OLLAMA_API_KEY', 'OLLAMA_MODELS_TO_PULL', 'TITLE_MODEL',
'OMEGA_NUM_CTX', 'VLLM_URL', 'VLLM_API_KEY', 'VLLM_MODEL')
function Get-CloudLink {
switch ("$env:AI_PROVIDER".ToLower()) {
'vllm' { return @{ Url = "$env:VLLM_URL"; Key = "$env:VLLM_API_KEY"; Probe = '/v1/models' } }
{ $_ -in 'openrouter', 'llamacpp' } { return $null }
}
# unset is ours too, the loader above drops .env.example's ollama:11434
if (-not $env:OLLAMA_URL -or $env:OLLAMA_URL -match '://(127\.0\.0\.1|localhost|\[::1\])(:|/|$)') { return $null }
return @{ Url = $env:OLLAMA_URL; Key = "$env:OLLAMA_API_KEY"; Probe = '/api/tags' }
}
# 8 s, a live tunnel answers in under one. a dead quick tunnel is
# a DNS failure or a cloudflare 530, a stale key the gate's 401.
# same thing to us, the lines are old.
function Test-CloudLink($link) {
if (-not $link.Url) { return $false }
# PS 5.1 still offers TLS 1.0 first and cloudflare won't talk to it
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$headers = @{}
if ($link.Key) { $headers['Authorization'] = "Bearer $($link.Key)" }
try {
Invoke-WebRequest -UseBasicParsing -Uri ($link.Url.TrimEnd('/') + $link.Probe) -Headers $headers -TimeoutSec 8 | Out-Null
return $true
} catch {
return $false
}
}
function Read-CloudLines {
Write-Host " paste the lines Step 3 of the notebook printed, then Enter on an empty line."
Write-Host " ${DIM}(just Enter keeps what .env has)${R}"
$saved = 0
while ($true) {
$line = [Console]::ReadLine()
if ($null -eq $line) { break }
$line = $line.Trim()
if (-not $line) { break }
$eq = $line.IndexOf('=')
if ($eq -lt 1) { continue }
$key = $line.Substring(0, $eq)
if ($key -cnotin $CloudKeys) { Note "skipped $key, the notebook never prints that one"; continue }
$val = $line.Substring($eq + 1)
$kept = @(Get-Content .env | Where-Object { $_ -notmatch "^$key=" })
Set-Content -Path .env -Value ($kept + "$key=$val")
Set-Item "env:$key" $val
$saved++
}
if ($saved) { Ok "saved $saved line(s) to .env" }
}
function Get-HfGguf([string]$repo, [string]$rev, [string]$quant) {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
try { $tree = Invoke-RestMethod -Uri "https://huggingface.co/api/models/$repo/tree/$rev" -TimeoutSec 10 } catch { return $null }
@($tree | Where-Object { $_.lfs -and $_.path -like "*.$quant.gguf" })[0].lfs
}
# a blob symlink is named by its sha. no Developer Mode = no
# symlinks, llama.cpp moves the file into the snapshot instead
function Get-CachedGguf([string]$repoDir, [string]$repo, [string]$quant) {
foreach ($f in @(Get-ChildItem -Path (Join-Path $repoDir 'snapshots\*\*') -Filter "*.$quant.gguf" -Force -ErrorAction SilentlyContinue)) {
$sha = if ($f.LinkType) { Split-Path -Leaf (@($f.Target)[0]) } else { (Get-HfGguf $repo $f.Directory.Name $quant).oid }
[pscustomobject]@{ Dir = $f.Directory.FullName; Sha = $sha }
}
}
function Get-OldSnapshots([string]$repoDir, [string]$keepDir) {
$linkSha = { Split-Path -Leaf (@($args[0].Target)[0]) }
$kept = @(Get-ChildItem -LiteralPath $keepDir -Force | Where-Object LinkType | ForEach-Object { & $linkSha $_ })
$snaps = @(Get-ChildItem -LiteralPath (Join-Path $repoDir 'snapshots') -Directory -Force | Where-Object { $_.FullName -ne $keepDir })
$files = foreach ($s in $snaps) {
foreach ($f in @(Get-ChildItem -LiteralPath $s.FullName -File -Force)) {
if (-not $f.LinkType) { $f; continue }
$sha = & $linkSha $f
if ($sha -notin $kept) { Get-Item -LiteralPath (Join-Path $repoDir "blobs\$sha") -Force -ErrorAction SilentlyContinue }
}
}
[pscustomobject]@{ Snapshots = $snaps; Files = @($files | Sort-Object FullName -Unique) }
}
function Get-ModelUpdate([string]$ref) {
if ("$env:LLAMACPP_OFFLINE" -match '^(on|1|true|yes)$') { return $null }
$repo, $quant = $ref -split ':', 2
if (-not $quant) { return $null }
$dir = Join-Path $Runtime "llama-cache\huggingface\hub\models--$($repo -replace '/', '--')"
$cached = @(Get-CachedGguf $dir $repo $quant)
if (-not $cached.Count) { return $null }
$remote = Get-HfGguf $repo 'main' $quant
if (-not $remote) { return $null }
[pscustomobject]@{ Ref = $ref; Repo = $repo; Quant = $quant; Dir = $dir; Sha = $remote.oid; Size = $remote.size; New = $cached.Sha -notcontains $remote.oid }
}
function Get-OldModelBytes($update) {
$keep = @(Get-CachedGguf $update.Dir $update.Repo $update.Quant | Where-Object { $_.Sha -eq $update.Sha })[0]
if (-not $keep) { return $null }
$old = Get-OldSnapshots $update.Dir $keep.Dir
[pscustomobject]@{ Old = $old; Bytes = [long]($old.Files | Measure-Object -Property Length -Sum).Sum }
}
$ChatModels = @("$(if ($env:LLAMACPP_MODEL_HF) { $env:LLAMACPP_MODEL_HF } else { 'efficiencyx/Jun-LoRA-E2B-GGUF:Q6_K' })" -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ })
# JunOS.exe has no console to ask in. it runs this first, asks in
# its own dialogs and hands the answers over as JUN_MODEL_UPDATE/PRUNE
if ($Action -eq 'model') {
$update = Get-ModelUpdate $ChatModels[0]
if ($update -and $update.New) {
$oldBytes = (@(Get-CachedGguf $update.Dir $update.Repo $update.Quant) | ForEach-Object {
$f = Get-ChildItem -Path (Join-Path $_.Dir "*.$($update.Quant).gguf") -Force | Select-Object -First 1
if ($f.LinkType) { (Get-Item -LiteralPath (Join-Path $update.Dir "blobs\$($_.Sha)") -Force -ErrorAction SilentlyContinue).Length } else { $f.Length }
} | Measure-Object -Sum).Sum
"model-update|$($update.Ref)|$($update.Size)|$oldBytes"
} elseif ($update) {
$old = Get-OldModelBytes $update
if ($old -and $old.Bytes -gt 0) { "model-old|$($update.Ref)|$($old.Bytes)" }
}
exit 0
}
if ($Action -eq 'link') {
Read-CloudLines
Note 'Stop then Start (or .\start.ps1) to bring her up with them'
exit 0
}
if ($Action -eq 'start' -and $env:JUN_LINK_CHECKED -ne '1') {
$link = Get-CloudLink
if ($link -and -not (Test-CloudLink $link)) {
if ($link.Url) {
Warn_ "her model server at $($link.Url) isn't answering. a new notebook run means a new URL and key"
} else {
Warn_ 'no link to her model server in .env yet (VLLM_URL is empty)'
}
if ([Environment]::UserInteractive -and -not [Console]::IsInputRedirected -and -not [Console]::IsOutputRedirected) {
Read-CloudLines
} else {
Note '.\start.ps1 link takes the new lines'
}
}
}
# same rules as Compare-JunVersion in install.ps1, -1/0/1. a
# -security tag sorts right above the same tag without it.
function Compare-JunVersion([string]$a, [string]$b) {
$pa = Split-JunVersion $a
$pb = Split-JunVersion $b
for ($i = 0; $i -lt 4; $i++) {
if ($pa.Core[$i] -ne $pb.Core[$i]) { return [Math]::Sign($pa.Core[$i] - $pb.Core[$i]) }
}
if ($pa.Pre -ne $pb.Pre) {
if (-not $pa.Pre) { return 1 }
if (-not $pb.Pre) { return -1 }
return [Math]::Sign([string]::CompareOrdinal($pa.Pre, $pb.Pre))
}
return [Math]::Sign([int]$pa.Security - [int]$pb.Security)
}
function Split-JunVersion([string]$v) {
$v = $v -replace '^v', ''
$security = $v -match '-security$'
$core, $pre = ($v -replace '-security$', '') -split '-', 2
$nums = @($core -split '\.' | ForEach-Object { $n = 0; [void][int]::TryParse($_, [ref]$n); $n }) + @(0, 0, 0, 0)
return @{ Core = $nums[0..3]; Pre = "$pre"; Security = $security }
}
# UPDATE_CHECK=on (the installer asks) and we're on a release
# tag: ask GitHub once what's newer on our channel. a newer
# -security release means a hole bad enough to stop her over
# (unauthenticated RCE class, see SECURITY.md), so no start
# without -IgnoreSecurity / JUN_IGNORE_SECURITY=1. exit 3 is what
# the manager window reads to offer the update. it never updates
# anything. main or a branch of your own is skipped.
function Get-UpdateNote {
if ("$env:UPDATE_CHECK" -notmatch '^(on|1|true|yes)$') { return '' }
if (-not (Get-Command git -ErrorAction SilentlyContinue)) { return '' }
# native stderr under Stop throws on 5.1, and "no tag here" is a normal answer
$ErrorActionPreference = 'Continue'
$current = (& git -C $JunRoot describe --tags --exact-match HEAD 2>$null) -join ''
if ($LASTEXITCODE -ne 0 -or -not $current) { return '' }
$origin = (& git -C $JunRoot remote get-url origin 2>$null) -join ''
if ($origin -notmatch '^(https://github\.com/|git@github\.com:)([^/]+/[^/]+?)(\.git)?/?$') { return '' }
$slug = $Matches[2]
$prereleases = "$env:JUN_CHANNEL" -in 'latest', 'experimental'
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$tags = @()
try {
$headers = @{ 'User-Agent' = 'JunOS-update-check'; 'Accept' = 'application/vnd.github+json' }
# assigned first, 5.1 hands a JSON array down the pipe as ONE object
$list = Invoke-RestMethod -Uri "https://api.github.com/repos/$slug/releases?per_page=20" -Headers $headers -TimeoutSec 8 -UseBasicParsing -ErrorAction Stop
$tags = @($list | Where-Object { -not $_.draft -and ($prereleases -or -not $_.prerelease) } | ForEach-Object { [string]$_.tag_name })
} catch {
# 60 anonymous API calls an hour per IP. the website's
# /releases/latest redirect has no cap but only knows the
# newest full release
try {
$req = [Net.HttpWebRequest]::Create("https://github.com/$slug/releases/latest")
$req.AllowAutoRedirect = $false
$req.UserAgent = 'JunOS-update-check'
$req.Timeout = 8000
$resp = $req.GetResponse()
try { $loc = [string]$resp.Headers['Location'] } finally { $resp.Close() }
if ($loc -match '/releases/tag/([^/?#]+)$') { $tags = @($Matches[1]) }
} catch {}
if (-not $tags) { Note "update check: couldn't reach GitHub, skipped"; return '' }
}
$newer = @($tags | Where-Object { (Compare-JunVersion $_ $current) -gt 0 })
if (-not $newer) { return '' }
$newest = $newer[0]
foreach ($t in $newer) { if ((Compare-JunVersion $t $newest) -gt 0) { $newest = $t } }
$security = $newer | Where-Object { $_ -match '-security$' } | Select-Object -First 1
if (-not $security) {
return "update: $newest is out, you're on $current. the Update button in the Jun OS app (or .\install.ps1 from this folder) updates her, chats and settings stay."
}
Write-Host ''
Fail_ "SECURITY: $security is out and fixes a vulnerability in the version you run ($current)."
Note "details: https://github.com/$slug/releases/tag/$security"
Note 'update: the Update button in the Jun OS app, or .\install.ps1 from this folder. chats and settings stay.'
if ($IgnoreSecurity -or "$env:JUN_IGNORE_SECURITY" -match '^(on|1|true|yes)$') {
Warn_ 'starting anyway, -IgnoreSecurity / JUN_IGNORE_SECURITY is set. update soon.'
return "reminder: you're on $current with a security fix out ($security). Update in the Jun OS app."
}
Fail_ 'NOT starting her on it. if she is still up from before, .\start.ps1 stop takes her down.'
Note '.\start.ps1 -IgnoreSecurity (or JUN_IGNORE_SECURITY=1 in .env) starts her anyway, at your own risk.'
exit 3
}
$UpdateNote = if ($Action -eq 'start') { Get-UpdateNote } else { '' }
$Port = if ($env:JUN_PORT) { $env:JUN_PORT } else { '8080' }
# php -S speaks plain HTTP only, so it sits on loopback behind
# caddy, which does the TLS on JUN_PORT. nothing but caddy should
# ever talk to this one.
$PhpPort = if ($env:JUN_PHP_PORT) { $env:JUN_PHP_PORT } else { '8079' }
$OllamaUrl = if ($env:OLLAMA_URL) { $env:OLLAMA_URL } else { 'http://127.0.0.1:11434' }
$OllamaRemote = $OllamaUrl -notmatch '://(127\.0\.0\.1|localhost|\[::1\])(:|/|$)'
# SiteUrl stays loopback whatever we bind to. it is what the
# browser opens, and that's this machine talking to itself.
$SiteUrl = "https://127.0.0.1:$Port"
$PhpUrl = "http://127.0.0.1:$PhpPort"
$BindAddr = if ($env:BIND_ADDR) { $env:BIND_ADDR.Trim() } else { '127.0.0.1' }
$LanHosts = @()
# this box's own addresses on the home network. skips the virtual
# adapters Hyper-V, WSL and Docker Desktop leave lying around,
# those aren't reachable from a phone and naming them just widens
# the Host allowlist for nothing.
function Get-PrivateIPv4 {
try {
@(Get-NetIPAddress -AddressFamily IPv4 -ErrorAction Stop |
Where-Object {
$_.IPAddress -match '^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[01])\.)' -and
$_.InterfaceAlias -notmatch 'vEthernet|WSL|Loopback|Hyper-V|VirtualBox|VMware'
} | Select-Object -ExpandProperty IPAddress -Unique)
} catch { @() }
}
function Test-Elevated {
try {
$me = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
return $me.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
} catch { return $false }
}
# binding to 0.0.0.0 is only half of it on windows. the firewall
# drops the inbound connection before php ever sees it, so the
# phone just hangs with no error anywhere. Private profile ONLY,
# this must not follow you onto cafe wifi. delete it with:
# Remove-NetFirewallRule -DisplayName "Jun OS (<port>)"
function Confirm-FirewallRule([string]$port) {
$ruleName = "Jun OS ($port)"
if (-not (Get-Command Get-NetFirewallRule -ErrorAction SilentlyContinue)) {
Warn_ "no NetSecurity module here, so open TCP $port on private networks yourself"
return
}
try {
if (Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue) {
Note "firewall rule '$ruleName' is already there"
return
}
} catch {
Warn_ "could not read the firewall rules: $($_.Exception.Message)"
return
}
if (Test-Elevated) {
try {
New-NetFirewallRule -DisplayName $ruleName -Direction Inbound -Action Allow `
-Protocol TCP -LocalPort $port -Profile Private -ErrorAction Stop | Out-Null
Ok "opened TCP $port on private networks (rule '$ruleName')"
} catch {
Warn_ "could not add the firewall rule: $($_.Exception.Message)"
}
} else {
Warn_ 'windows firewall will block the phone. run this once in an admin PowerShell:'
Note "New-NetFirewallRule -DisplayName '$ruleName' -Direction Inbound -Action Allow -Protocol TCP -LocalPort $port -Profile Private"
}
}
if ($Action -eq 'start' -and $BindAddr -notin @('127.0.0.1', 'localhost', '::1')) {
$LanHosts = @(Get-PrivateIPv4)
# php's built-in server is single-worker on windows
# (PHP_CLI_SERVER_WORKERS is a unix-only knob), so the phone and
# the desktop are not two users, they are one queue. whoever asks
# second waits out the first reply's whole stream. docker doesn't
# have this problem, php-fpm forks.
Note 'one request at a time on windows - a second device waits for the first reply to finish'
Note 'off loopback even the first account needs OMEGA_REGISTRATION_KEY from .env'
Confirm-FirewallRule $Port
}
$Provider = if ($env:AI_PROVIDER) { $env:AI_PROVIDER.ToLower() } else { 'ollama' }
if ($Provider -notin 'ollama', 'openrouter', 'llamacpp', 'vllm') { $Provider = 'ollama' }
$LlamaPort = if ($env:LLAMACPP_PORT) { $env:LLAMACPP_PORT } else { '8081' }
$LlamacppUrl = if ($env:LLAMACPP_URL) { $env:LLAMACPP_URL } else { "http://127.0.0.1:$LlamaPort" }
$GpuDevices = if ($env:GPU_DEVICES) { $env:GPU_DEVICES.Trim() } else { '' }
if (-not $GpuDevices -or $GpuDevices -eq 'auto') {
$GpuDevices = Get-GpuOrder
} elseif ($GpuDevices -eq 'all') {
$GpuDevices = ''
}
# an empty CUDA_VISIBLE_DEVICES means NO GPUs, not all of them,
# so only set it when we actually have a list. the model servers
# pick it up from here.
if ($GpuDevices) { $env:CUDA_VISIBLE_DEVICES = $GpuDevices }
$TensorParallel = $env:TENSOR_PARALLEL -match '^(on|1|true|yes)$'
# a hand-set value wins, same as start.sh
if ($Provider -eq 'ollama' -and -not $env:OMEGA_GPU_VRAM_MB) {
$vramMb = Get-VramMb
if ($vramMb) { $env:OMEGA_GPU_VRAM_MB = "$vramMb" }
}
if ($Action -eq 'start') {
if ($GpuDevices -and ($GpuDevices -split ',').Count -gt 1) {
Note "GPU order (largest VRAM first): $GpuDevices"
}
if ($TensorParallel) { Note 'tensor parallel: one model split across all GPUs' }
}
function Read-Pids {
if (Test-Path $PidFile) {
try { return Get-Content $PidFile -Raw | ConvertFrom-Json } catch {}
}
return $null
}
function Get-TrackedProcess([object]$pids, [string]$name) {
if (-not $pids -or -not $pids.$name) { return $null }
$p = Get-Process -Id $pids.$name -ErrorAction SilentlyContinue
if ($p) { return $p }
return $null
}
# what pids.json lost track of still holds JUN_PORT and the files
# under runtime\, so the uninstaller can't delete caddy.exe
function Stop-Strays([int[]]$keep = @()) {
# an earlier start through the 8.3 alias left that in their command lines
$roots = @($JunRoot, (ConvertTo-ShortPath $JunRoot)) | Select-Object -Unique |
ForEach-Object { $_.TrimEnd('\') + '\' }
Get-CimInstance Win32_Process -ErrorAction SilentlyContinue `
-Filter "Name='caddy.exe' OR Name='php.exe' OR Name='python.exe' OR Name='pythonw.exe' OR Name='llama-server.exe'" |
Where-Object {
$proc = $_
$proc.ProcessId -ne $PID -and $proc.ProcessId -notin $keep -and $proc.ParentProcessId -notin $keep -and
@($roots | Where-Object {
($proc.ExecutablePath -and $proc.ExecutablePath.StartsWith($_, [StringComparison]::OrdinalIgnoreCase)) -or
($proc.CommandLine -and $proc.CommandLine.IndexOf($_, [StringComparison]::OrdinalIgnoreCase) -ge 0)
}).Count -gt 0
} | ForEach-Object {
try {
Stop-Process -Id $_.ProcessId -Force -ErrorAction Stop
Ok "stopped untracked $($_.Name) (pid $($_.ProcessId))"
} catch {
Warn_ "could not stop $($_.Name) (pid $($_.ProcessId)): $($_.Exception.Message)"
}
}
}
# caddy gives the same WSAEACCES for a port another app holds and
# one windows reserved, so say which before it starts
function Get-PortBlocker([string]$port, [string]$addr) {
if ($addr -eq 'localhost') { $addr = '127.0.0.1' }
if (Get-Command Get-NetTCPConnection -ErrorAction SilentlyContinue) {
for ($try = 0; $try -lt 6; $try++) {
$owners = @(Get-NetTCPConnection -LocalPort ([int]$port) -State Listen -ErrorAction SilentlyContinue |
Where-Object { $_.LocalAddress -in '0.0.0.0', '::', $addr -or $addr -in '0.0.0.0', '::' } |
ForEach-Object { Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue } |
Sort-Object Id -Unique)
if (-not $owners) { break }
Start-Sleep -Milliseconds 500
}
if ($owners) {
return ($owners | ForEach-Object {
$path = if ($_.Path) { " ($($_.Path))" } else { ' (no path, a windows service or something run as admin)' }
"$($_.ProcessName) pid $($_.Id)$path"
}) -join ', '
}
}
foreach ($line in (netsh interface ipv4 show excludedportrange protocol=tcp)) {
if ($line -match '^\s*(\d+)\s+(\d+)' -and [int]$port -ge [int]$Matches[1] -and [int]$port -le [int]$Matches[2]) {
return "windows itself, it reserved ports $($Matches[1])-$($Matches[2]) (Hyper-V, WSL or Docker Desktop do that)"
}
}
return $null
}
function Test-Http([string]$url, [int]$timeoutSec = 2) {
try {
Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec $timeoutSec | Out-Null
return $true
} catch { return $false }
}
# PS 5.1's Invoke-WebRequest has no -SkipCertificateCheck and our
# cert comes from a local CA it may not trust, so it just refuses
# caddy. a bare handshake that takes any cert answers the only
# question we have: is caddy up on the port and doing TLS.
function Test-Tls([string]$port) {
$tcp = [Net.Sockets.TcpClient]::new()
try {
if (-not $tcp.ConnectAsync('127.0.0.1', [int]$port).Wait(2000)) { return $false }
$ssl = [Net.Security.SslStream]::new($tcp.GetStream(), $false,
[Net.Security.RemoteCertificateValidationCallback] { $true })
$ssl.AuthenticateAsClient('localhost', $null, [Security.Authentication.SslProtocols]::Tls12, $false)
return $true
} catch { return $false } finally { $tcp.Dispose() }
}
# the draft depth in .env is a measurement, and it only describes
# the card it was measured on. swap the GPU and that number is
# about hardware that left the building. so hold the stamp the
# tuner wrote against what's in the box NOW and measure again when
# the two don't match.
function Invoke-MtpRecheck {
# on the llamacpp side the tuner restarts the stack through
# start.ps1, which is us. without this we start a tune inside
# a tune. forever.
if ($env:MTP_AUTOTUNE_RUNNING) { return }
if ($env:MTP_AUTOTUNE -and $env:MTP_AUTOTUNE.ToLower() -match '^(off|0|false|no)$') { return }
# no stamp means no tune ever finished on this box, so there's
# nothing to compare and nothing to nag about.
$tuned = $env:MTP_TUNED_GPU
if (-not $tuned) { return }
$sig = Get-GpuSignature
if (-not $sig -or $sig -eq $tuned) { return }
$drafter = ''
switch ($Provider) {
'llamacpp' { $drafter = $env:LLAMACPP_MTP }
'ollama' { $drafter = $env:OLLAMA_MTP }
default { return }
}
# MTP is off so there's no depth to measure. the tuner would
# just die on it and we'd be back here every single boot.
if (-not $drafter) { return }
Step 'the GPU changed since MTP was tuned'
Note "tuned on: $tuned"
Note "here now: $sig"
Note 'the draft depth in .env was measured on a card that is not in this box any more, so we are measuring it again'
Note 'a few minutes on ollama, considerably longer on llamacpp where every depth needs a llama-server restart'
Note 'set MTP_AUTOTUNE=off in .env to skip this'
# /api/tags answers the moment ollama is up, which tells us
# NOTHING about whether the drafter is pulled yet. the tuner
# needs that blob on disk or it dies with "could not find the
# drafter blob", so wait until ollama show admits it has one.
$deadline = (Get-Date).AddMinutes(5)
$ready = $false
while ((Get-Date) -lt $deadline) {
if ($Provider -eq 'llamacpp') {
if (Test-Http "$LlamacppUrl/health") { $ready = $true; break }
} elseif (Test-Http "$OllamaUrl/api/tags") {
# PS 7.4 turns a non-zero exit from a native command into
# a throw under ErrorActionPreference Stop, and "not pulled
# yet" is the NORMAL answer in here, not an error.
try {
& ollama show --modelfile $drafter 2>$null | Out-Null
if ($LASTEXITCODE -eq 0) { $ready = $true; break }
} catch {}
}
Start-Sleep -Seconds 5
}
# leave the stamp stale ON PURPOSE. it's the only thing that
# makes the next boot try again, and a pull that's still
# running now is probably done by then.
if (-not $ready) {
Warn_ 'the drafter is not here yet, so the re-tune is skipped. run .\mtp-autotune.ps1 by hand once it has finished pulling.'
return
}
try {
$env:MTP_AUTOTUNE_RUNNING = '1'
& "$JunRoot\mtp-autotune.ps1"
} catch {
Warn_ "autotune did not finish, run .\mtp-autotune.ps1 by hand ($_)"
} finally {
Remove-Item env:MTP_AUTOTUNE_RUNNING -ErrorAction SilentlyContinue
# the tuner just wrote .env and the copy we read at startup
# is older than that. drop it, so a second start.ps1 in the
# same session picks the new stamp up off disk instead of
# re-running the whole sweep against a stale value.
Remove-Item env:MTP_TUNED_GPU -ErrorAction SilentlyContinue
}
}
if ($Action -eq 'stop') {
Step 'stopping services'
$pids = Read-Pids
foreach ($name in $Services) {
$p = Get-TrackedProcess $pids $name
if (-not $p) { continue }
try {
Stop-Process -Id $p.Id -Force -ErrorAction Stop
Ok "stopped $name (pid $($p.Id))"
} catch {
Warn_ "could not stop $name (pid $($p.Id)): $($_.Exception.Message)"
}
}
Stop-Strays
Remove-Item $PidFile -ErrorAction SilentlyContinue
Write-Host ''
Ok 'all services stopped'
exit 0
}
# the CA goes in the user's Root store, not the machine's. no
# admin needed, windows asks on its own with a "do you want to
# install this certificate" dialog, and that dialog is the consent.
# chrome and edge read that store. firefox only when it imports
# windows roots (security.enterprise_roots.enabled).
# tls\trusted is the thumbprint we added, so a new CA swaps the old
# one out and uninstall.ps1 knows which one to take back.
function Update-CaTrust([bool]$firstTime) {
$caFile = Join-Path $CertDir 'ca.pem'
$mark = Join-Path $CertDir 'trusted'
if (-not (Test-Path $caFile)) { return $false }
$ca = [Security.Cryptography.X509Certificates.X509Certificate2]::new($caFile)
$old = if (Test-Path $mark) { (Get-Content $mark -Raw).Trim() } else { '' }
if (-not $old -and -not $firstTime) { return $false }
$store = [Security.Cryptography.X509Certificates.X509Store]::new('Root', 'CurrentUser')
$store.Open('ReadWrite')
try {
if ($store.Certificates.Find('FindByThumbprint', $ca.Thumbprint, $false).Count -gt 0) {
Set-Content $mark $ca.Thumbprint
return $true
}
# same thumbprint and gone from the store = someone took it
# out by hand. that's a no, don't ask on every start.
if ($old -eq $ca.Thumbprint -and -not $firstTime) {
Remove-Item $mark
return $false
}
if ($old -and $old -ne $ca.Thumbprint) {
Note 'the local CA changed, swapping the old one out of the windows trust store'
foreach ($c in $store.Certificates.Find('FindByThumbprint', $old, $false)) { $store.Remove($c) }
}
Remove-Item $mark -ErrorAction SilentlyContinue
Note "windows will ask whether to install a certificate from `"$($ca.GetNameInfo('SimpleName', $false))`". yes = no more browser warning"
try {
$store.Add($ca)
} catch {
Warn_ "not trusted ($($_.Exception.Message)). ./start.ps1 trust asks again"
return $false
}
Set-Content $mark $ca.Thumbprint
Ok 'trusted - restart the browser if it was open'
return $true
} finally {
$store.Close()
}
}
if ($Action -eq 'trust') {
Step 'trust the local CA'
if (-not (Test-Path (Join-Path $CertDir 'ca.pem'))) {
Warn_ 'no CA yet. start her once first: ./start.ps1'
exit 1
}
if (Update-CaTrust $true) { exit 0 } else { exit 1 }
}
# JunBridge reads ApiUrl from its cfg and JunOS's own CA from
# JunBridge.ca.pem, so a new JUN_PORT or CA reaches the game next start
function Update-BridgeMod {
$gameDir = $env:JUN_GAME_DIR
if (-not $gameDir -or -not (Test-Path -LiteralPath (Join-Path $gameDir 'Mods\JunBridge.dll'))) { return $false }
$userData = Join-Path $gameDir 'UserData'
New-Item -ItemType Directory -Force -Path $userData | Out-Null
$ca = Join-Path $CertDir 'ca.pem'
if (Test-Path -LiteralPath $ca) { Copy-Item -LiteralPath $ca -Destination (Join-Path $userData 'JunBridge.ca.pem') -Force }
$cfg = Join-Path $userData 'JunBridge.cfg'
$line = 'ApiUrl = "' + $SiteUrl + '"'
$text = if (Test-Path -LiteralPath $cfg) { [IO.File]::ReadAllText($cfg) } else { '' }
if ($text -match '(?m)^ApiUrl\s*=') {
$text = [regex]::Replace($text, '(?m)^ApiUrl\s*=.*?(?=\r?$)', $line)
} elseif ($text -match '(?m)^\[Jun\]') {
$text = [regex]::Replace($text, '(?m)^\[Jun\][ \t]*(?=\r?$)', "[Jun]`r`n$line", 1)
} else {
$text = "[Jun]`r`n$line`r`n`r`n" + $text
}
# no BOM, 5.1's -Encoding UTF8 writes one
[IO.File]::WriteAllText($cfg, $text)
return $true
}
if ($Action -eq 'bridge') {
if (-not (Update-BridgeMod)) {
Warn_ 'no JunBridge mod in JUN_GAME_DIR. re-run install.ps1 with JUN_BRIDGE=1'
exit 1
}
Ok 'JunBridge points at her. start the game, it asks you to sign in'
exit 0
}
function Get-PhpExe {
$exe = Join-Path $Runtime 'php\php.exe'
if (Test-Path $exe) { return $exe }
$cmd = Get-Command php -ErrorAction SilentlyContinue
if (-not $cmd) { throw 'PHP not found. Run install.ps1 first (it puts a portable PHP under runtime\php).' }
return $cmd.Source
}
# .\start.ps1 phone: the QR the android app scans for LAN mode. asks
# before turning LAN_INFERENCE on, BIND_ADDR=0.0.0.0 opens her up
if ($Action -eq 'phone') {
$phpExe = Get-PhpExe
$token = "$env:JUN_LAN_TOKEN"
$loopback = $BindAddr -in @('127.0.0.1', 'localhost', '::1')
$need = @()
if ("$env:LAN_INFERENCE" -notmatch '^(on|1|true|yes)$') { $need += 'LAN_INFERENCE=on' }
if ($token.Length -lt 32) { $need += 'a new JUN_LAN_TOKEN' }
if ($loopback) { $need += 'BIND_ADDR=0.0.0.0' }
if ($need.Count) {
Note "the phone needs: $($need -join ', ')"
Note 'BIND_ADDR=0.0.0.0 lets every device on this network reach her login page, and her model with the token'
if (-not $Yes) {
if (-not [Environment]::UserInteractive -or [Console]::IsInputRedirected) {
Fail_ 'set those in .env and run .\start.ps1 phone again'
exit 1
}
$ans = Read-Host ' write them to .env and restart her? [Y/n]'
if ($ans -and $ans -notmatch '^(y|yes)$') { exit 1 }
}
if ($token.Length -lt 32) {
$bytes = [byte[]]::new(32)
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
$token = ($bytes | ForEach-Object { $_.ToString('x2') }) -join ''
}
$set = [ordered]@{ LAN_INFERENCE = 'on'; JUN_LAN_TOKEN = $token }
if ($loopback) { $set.BIND_ADDR = '0.0.0.0'; $BindAddr = '0.0.0.0' }
if (-not (Test-Path .env)) { New-Item -ItemType File .env | Out-Null }
foreach ($k in $set.Keys) {
$kept = @(Get-Content .env | Where-Object { $_ -notmatch "^$k=" })
Set-Content -Path .env -Value ($kept + "$k=$($set[$k])")
Set-Item "env:$k" $set[$k]
}
}
# php itself, not .env. an edit nobody restarted for would hand
# the phone a token php has never heard of
$live = $false
try {
Invoke-WebRequest -UseBasicParsing -Uri "$PhpUrl/api/model.php" -TimeoutSec 5 `
-Headers @{ Authorization = "Bearer $token" } | Out-Null
$live = $true
} catch {}
if (-not $live) {
Step 'restarting her with LAN mode on'
# NOT | Out-Host. her services inherit that pipe and keep it
# open, Out-Host waits for EOF forever. -NoNewWindow passes ours
foreach ($step in 'stop', 'start -NoBrowser') {
$p = Start-Process (Get-Process -Id $PID).Path -NoNewWindow -PassThru `
-ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $step"
$null = $p.Handle
$p.WaitForExit()
}
if ($p.ExitCode -ne 0) { Fail_ 'she did not come back up, see above'; exit 1 }
}
Confirm-FirewallRule $Port
$caFile = Join-Path $CertDir 'ca.pem'
$ca = if (Test-Path $caFile) {
& $phpExe -r 'echo openssl_x509_fingerprint(file_get_contents($argv[1]), ''sha256'');' -- $caFile
}
if (-not $ca) { Fail_ "couldn't read her local CA at $caFile"; exit 1 }
$hosts = @(Get-PrivateIPv4) + @($env:OMEGA_EXTRA_HOSTS -split '[,\s]+' | Where-Object { $_ })
if (-not $hosts) { Fail_ 'no private address on this machine for the phone to reach'; exit 1 }
$enc = { param($s) [Uri]::EscapeDataString($s) }
$link = "junos://pair?mode=lan&name=$(& $enc $env:COMPUTERNAME)&token=$token&ca=$ca"
foreach ($h in $hosts) { $link += "&url=$(& $enc "https://${h}:$Port/api/model.php")" }
# drawn here from 0s and 1s, not as ANSI. the old console
# without VT would print the escape codes as garbage
$rows = @(& $phpExe (Join-Path $JunRoot 'tools\qr.php') $link rows)
$blank = '0' * $rows[0].Length
$rows = @($blank) * 4 + $rows + @($blank) * 5
Write-Host ''
for ($y = 0; $y + 1 -lt $rows.Count; $y += 2) {
$line = ' '
for ($x = 0; $x -lt $rows[$y].Length; $x++) {
$top = $rows[$y][$x] -eq '1'; $bottom = $rows[$y + 1][$x] -eq '1'
$line += if ($top -and $bottom) { [char]0x2588 } elseif ($top) { [char]0x2580 } elseif ($bottom) { [char]0x2584 } else { ' ' }
}
Write-Host ' ' -NoNewline
Write-Host ($line + ' ') -ForegroundColor Black -BackgroundColor White
}
Write-Host ''
Note 'scan it in the Jun OS app (LAN, then Scan), or with the phone camera. same wifi as this PC.'
Note 'no camera? get this link onto the phone and tap Paste link in the app:'
Write-Host " $link"
Note 'the code holds the token: anyone who scans it gets her model. a new JUN_LAN_TOKEN in .env cuts everyone off.'
exit 0
}
if ($Action -eq 'status') {
Step 'service status'
$pids = Read-Pids
foreach ($name in $Services) {
$p = Get-TrackedProcess $pids $name
if ($p) {
Ok ("{0,-10} running (pid {1})" -f $name, $p.Id)
} else {
Note ("{0,-10} not running" -f $name)
}
}
if ((Test-Http $PhpUrl) -and (Test-Tls $Port)) {
Ok "web UI $SiteUrl"
} else {
Warn_ 'web UI not responding'
}
exit 0
}
New-Item -ItemType Directory -Force -Path $Runtime, $LogDir, $StateDir, (Join-Path $StateDir 'rl') | Out-Null
$newPids = @{}
$oldPids = Read-Pids
function Start-Tracked([string]$name, [string]$exe, [string[]]$exeArgs) {
$out = Join-Path $LogDir "$name.log"
$err = Join-Path $LogDir "$name.err.log"
# -ArgumentList just joins the array with spaces, no quoting. so
# an install under C:\Users\Some Name\ hands php `-t C:\Users\Some`
# and it dies with "Directory ... does not exist". quote it ourselves
$quoted = $exeArgs | ForEach-Object { if ($_ -match '\s') { "`"$_`"" } else { $_ } }
$p = Start-Process -FilePath $exe -ArgumentList $quoted -WindowStyle Hidden `
-RedirectStandardOutput $out -RedirectStandardError $err -PassThru
# touch Handle NOW. without an open handle ExitCode comes back
# empty once the process is gone, "exited (code )" tells nobody anything
$null = $p.Handle
$script:newPids[$name] = $p.Id
return $p
}
# if an Ollama server is already up (the desktop app autostarts
# one) just use it. otherwise launch `ollama serve` ourselves,
# with the model store inside the install folder so the weights
# go away with it on uninstall. an OLLAMA_URL off this machine (the
# colab notebook's tunnel) gets none of that, no install check and
# no 8 GB pull of weights that would never get used here.
if ($Provider -eq 'ollama' -and $OllamaRemote) {
Ok "using Ollama at $OllamaUrl (not on this machine, nothing to start here)"
} elseif ($Provider -eq 'ollama') {
if (-not (Get-Command ollama -ErrorAction SilentlyContinue)) {
throw 'ollama is not installed. Run install.ps1 first (it installs Ollama for you).'
}