forked from Fei-Away/Codex-Dream-Skin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-windows.ps1
More file actions
806 lines (747 loc) · 31 KB
/
Copy pathcommon-windows.ps1
File metadata and controls
806 lines (747 loc) · 31 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
. (Join-Path $PSScriptRoot 'config-utf8.ps1')
function Enter-DreamSkinOperationLock {
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$mutex = [System.Threading.Mutex]::new($false, "Local\CodexDreamSkin.$sid.Operation")
$acquired = $false
try {
$acquired = $mutex.WaitOne(0)
} catch [System.Threading.AbandonedMutexException] {
$acquired = $true
}
if (-not $acquired) {
$mutex.Dispose()
throw 'Another Codex Dream Skin install, start, restore, or verify operation is already running.'
}
return $mutex
}
function Exit-DreamSkinOperationLock {
param([Parameter(Mandatory = $true)][System.Threading.Mutex]$Mutex)
try { $Mutex.ReleaseMutex() } finally { $Mutex.Dispose() }
}
function Assert-DreamSkinPort {
param([Parameter(Mandatory = $true)][int]$Port)
if ($Port -lt 1024 -or $Port -gt 65535) { throw "Port must be between 1024 and 65535: $Port" }
}
function Test-DreamSkinPathEqual {
param([string]$Left, [string]$Right)
if (-not $Left -or -not $Right) { return $false }
try {
return ([System.IO.Path]::GetFullPath($Left).TrimEnd('\') -ieq [System.IO.Path]::GetFullPath($Right).TrimEnd('\'))
} catch {
return $false
}
}
function Test-DreamSkinPathWithin {
param([string]$Path, [string]$Root)
if (-not $Path -or -not $Root) { return $false }
try {
$fullPath = [System.IO.Path]::GetFullPath($Path)
$prefix = [System.IO.Path]::GetFullPath($Root).TrimEnd('\') + '\'
return $fullPath.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)
} catch {
return $false
}
}
function Get-DreamSkinRuntimeEnginePaths {
param([string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin'))
$root = Join-Path ([System.IO.Path]::GetFullPath($StateRoot)) 'engine'
$scripts = Join-Path $root 'scripts'
return [pscustomobject]@{
Root = $root
Scripts = $scripts
Start = Join-Path $scripts 'start-dream-skin.ps1'
Restore = Join-Path $scripts 'restore-dream-skin.ps1'
Tray = Join-Path $scripts 'tray-dream-skin.ps1'
}
}
function Test-DreamSkinTrayActive {
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$mutex = [System.Threading.Mutex]::new($false, "Local\CodexDreamSkin.$sid.Tray")
$acquired = $false
try {
try { $acquired = $mutex.WaitOne(0) } catch [System.Threading.AbandonedMutexException] {
$acquired = $true
}
if ($acquired) {
$mutex.ReleaseMutex()
$acquired = $false
return $false
}
return $true
} finally {
if ($acquired) { try { $mutex.ReleaseMutex() } catch {} }
$mutex.Dispose()
}
}
function Assert-DreamSkinRuntimeTree {
param([Parameter(Mandatory = $true)][string]$Path)
$root = [System.IO.Path]::GetFullPath($Path)
if (-not (Test-Path -LiteralPath $root -PathType Container)) {
throw "Dream Skin runtime directory does not exist: $root"
}
if (-not (Get-Command Assert-DreamSkinNoReparseComponents -ErrorAction SilentlyContinue)) {
throw 'Dream Skin managed-path validation is unavailable.'
}
Assert-DreamSkinNoReparseComponents -Path $root
foreach ($item in Get-ChildItem -LiteralPath $root -Recurse -Force -ErrorAction Stop) {
if (($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
throw "Dream Skin runtime contains a junction or symbolic link: $($item.FullName)"
}
}
}
function Remove-DreamSkinRuntimeTree {
param(
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][string]$StateRoot
)
$fullPath = [System.IO.Path]::GetFullPath($Path)
$fullStateRoot = [System.IO.Path]::GetFullPath($StateRoot)
if (-not (Test-DreamSkinPathWithin -Path $fullPath -Root $fullStateRoot)) {
throw "Refusing to remove a runtime path outside the Dream Skin state root: $fullPath"
}
if (-not (Test-Path -LiteralPath $fullPath)) { return }
Assert-DreamSkinRuntimeTree -Path $fullPath
Remove-Item -LiteralPath $fullPath -Recurse -Force -ErrorAction Stop
}
function Install-DreamSkinRuntimeEngine {
param(
[Parameter(Mandatory = $true)][string]$SkillRoot,
[Parameter(Mandatory = $true)][string]$StateRoot
)
if (-not (Get-Command Ensure-DreamSkinManagedDirectory -ErrorAction SilentlyContinue)) {
throw 'Dream Skin managed-directory validation is unavailable.'
}
$sourceRoot = [System.IO.Path]::GetFullPath($SkillRoot)
$fullStateRoot = [System.IO.Path]::GetFullPath($StateRoot)
$engine = Get-DreamSkinRuntimeEnginePaths -StateRoot $fullStateRoot
$required = @(
'assets\dream-reference.jpg',
'assets\dream-skin.css',
'assets\renderer-inject.js',
'assets\theme.json',
'scripts\common-windows.ps1',
'scripts\config-utf8.ps1',
'scripts\image-metadata.mjs',
'scripts\injector.mjs',
'scripts\install-dream-skin.ps1',
'scripts\restore-dream-skin.ps1',
'scripts\start-dream-skin.ps1',
'scripts\theme-windows.ps1',
'scripts\tray-dream-skin.ps1',
'scripts\verify-dream-skin.ps1'
)
foreach ($relative in $required) {
if (-not (Test-Path -LiteralPath (Join-Path $sourceRoot $relative) -PathType Leaf)) {
throw "Dream Skin runtime source is incomplete: $relative"
}
}
foreach ($directoryName in @('assets', 'scripts')) {
$sourceDirectory = Join-Path $sourceRoot $directoryName
if ((Test-DreamSkinPathEqual -Left $fullStateRoot -Right $sourceDirectory) -or
(Test-DreamSkinPathWithin -Path $fullStateRoot -Root $sourceDirectory)) {
throw "Dream Skin state root cannot be created inside its runtime source: $fullStateRoot"
}
Assert-DreamSkinRuntimeTree -Path $sourceDirectory
}
Ensure-DreamSkinManagedDirectory -Path $fullStateRoot -Root $fullStateRoot
$token = [guid]::NewGuid().ToString('N')
$stagingRoot = Join-Path $fullStateRoot ".engine-staging-$token"
$backupRoot = Join-Path $fullStateRoot ".engine-backup-$token"
Ensure-DreamSkinManagedDirectory -Path $stagingRoot -Root $fullStateRoot
try {
foreach ($directoryName in @('assets', 'scripts')) {
Copy-Item -LiteralPath (Join-Path $sourceRoot $directoryName) -Destination $stagingRoot `
-Recurse -Force -ErrorAction Stop
}
Assert-DreamSkinRuntimeTree -Path $stagingRoot
foreach ($relative in $required) {
if (-not (Test-Path -LiteralPath (Join-Path $stagingRoot $relative) -PathType Leaf)) {
throw "Staged Dream Skin runtime is incomplete: $relative"
}
}
$sourcePrefix = $sourceRoot.TrimEnd('\') + '\'
$sourceFiles = @(
Get-ChildItem -LiteralPath (Join-Path $sourceRoot 'assets'), (Join-Path $sourceRoot 'scripts') `
-Recurse -File -Force -ErrorAction Stop
)
$stagedFiles = @(
Get-ChildItem -LiteralPath (Join-Path $stagingRoot 'assets'), (Join-Path $stagingRoot 'scripts') `
-Recurse -File -Force -ErrorAction Stop
)
if ($sourceFiles.Count -ne $stagedFiles.Count) {
throw 'Staged Dream Skin runtime file count does not match its source.'
}
foreach ($sourceFile in $sourceFiles) {
$relative = $sourceFile.FullName.Substring($sourcePrefix.Length)
$stagedFile = Join-Path $stagingRoot $relative
if (-not (Test-Path -LiteralPath $stagedFile -PathType Leaf) -or
(Get-FileHash -Algorithm SHA256 -LiteralPath $sourceFile.FullName).Hash -cne
(Get-FileHash -Algorithm SHA256 -LiteralPath $stagedFile).Hash) {
throw "Staged Dream Skin runtime failed hash verification: $relative"
}
}
$hasBackup = $false
if (Test-Path -LiteralPath $engine.Root) {
Assert-DreamSkinRuntimeTree -Path $engine.Root
Move-Item -LiteralPath $engine.Root -Destination $backupRoot -ErrorAction Stop
$hasBackup = $true
}
try {
Move-Item -LiteralPath $stagingRoot -Destination $engine.Root -ErrorAction Stop
} catch {
$installError = $_.Exception.Message
if ($hasBackup -and -not (Test-Path -LiteralPath $engine.Root)) {
try {
Move-Item -LiteralPath $backupRoot -Destination $engine.Root -ErrorAction Stop
$hasBackup = $false
} catch {
throw "Dream Skin runtime update failed and its previous engine could not be restored. Backup preserved at ${backupRoot}: $installError"
}
}
throw
}
if ($hasBackup) {
try { Remove-DreamSkinRuntimeTree -Path $backupRoot -StateRoot $fullStateRoot } catch {
try {
Write-Warning "Installed the new runtime but could not remove its previous backup: $($_.Exception.Message)"
} catch {
# Cleanup must never make a committed runtime update look unsuccessful.
}
}
}
return Get-DreamSkinRuntimeEnginePaths -StateRoot $fullStateRoot
} finally {
if (Test-Path -LiteralPath $stagingRoot) {
try { Remove-DreamSkinRuntimeTree -Path $stagingRoot -StateRoot $fullStateRoot } catch {
try {
Write-Warning "Could not remove the staged Dream Skin runtime: $($_.Exception.Message)"
} catch {
# Cleanup must never mask the runtime installation result.
}
}
}
}
}
function Test-DreamSkinCommandLineToken {
param([string]$CommandLine, [string]$Token)
if (-not $CommandLine -or -not $Token) { return $false }
$pattern = '(?i)(?:^|[\s"])' + [regex]::Escape($Token) + '(?=$|[\s"])'
return [regex]::IsMatch($CommandLine, $pattern)
}
function ConvertTo-DreamSkinProcessArgument {
param([Parameter(Mandatory = $true)][AllowEmptyString()][string]$Value)
if ($Value.Contains('"')) { throw 'Process arguments containing a double quote are not supported.' }
if ($Value.Length -eq 0) { return '""' }
if ($Value -notmatch '\s') { return $Value }
$escaped = [regex]::Replace($Value, '(\\+)$', '$1$1')
return '"' + $escaped + '"'
}
function ConvertTo-DreamSkinArgumentLine {
param([AllowEmptyCollection()][string[]]$Arguments = @())
return (($Arguments | ForEach-Object { ConvertTo-DreamSkinProcessArgument -Value $_ }) -join ' ')
}
function Get-DreamSkinProcessExecutablePath {
param([Parameter(Mandatory = $true)][object]$ProcessInfo)
if ($ProcessInfo.ExecutablePath) { return "$($ProcessInfo.ExecutablePath)" }
try {
$process = Get-Process -Id ([int]$ProcessInfo.ProcessId) -ErrorAction Stop
if ($process.Path) { return "$($process.Path)" }
return "$($process.MainModule.FileName)"
} catch {
return $null
}
}
# Windows PowerShell 5.1 promotes redirected native-command stderr lines to
# ErrorRecords; while $ErrorActionPreference is 'Stop' the first stderr line
# becomes a terminating NativeCommandError before the exit code can be read.
# Run the command with the preference relaxed and report output + exit code.
function Invoke-DreamSkinNative {
param(
[Parameter(Mandatory = $true)][string]$FilePath,
[string[]]$ArgumentList = @(),
[switch]$DiscardStderr
)
$previousPreference = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
if ($DiscardStderr) {
$nativeOutput = @(& $FilePath @ArgumentList 2>$null)
} else {
$nativeOutput = @(& $FilePath @ArgumentList 2>&1)
}
$exitCode = $LASTEXITCODE
$output = @($nativeOutput | ForEach-Object { "$_" })
return [pscustomobject]@{ Output = $output; ExitCode = $exitCode }
} finally {
$ErrorActionPreference = $previousPreference
}
}
function Get-DreamSkinNodeRuntime {
param([int]$MinimumMajor = 22)
$command = Get-Command node.exe -ErrorAction SilentlyContinue
if (-not $command) { $command = Get-Command node -ErrorAction SilentlyContinue }
if (-not $command) { throw "Node.js $MinimumMajor or newer is required and was not found in PATH." }
$versionProbe = Invoke-DreamSkinNative -FilePath $command.Source -ArgumentList @('-p', 'process.versions.node') -DiscardStderr
$version = ($versionProbe.Output -join '').Trim()
if ($versionProbe.ExitCode -ne 0 -or -not $version) { throw 'The Node.js runtime could not be validated.' }
$pathProbe = Invoke-DreamSkinNative -FilePath $command.Source -ArgumentList @('-p', 'process.execPath') -DiscardStderr
$runtimePath = ($pathProbe.Output -join '').Trim()
if ($pathProbe.ExitCode -ne 0 -or -not $runtimePath -or -not (Test-Path -LiteralPath $runtimePath)) {
throw 'The Node.js executable path could not be validated.'
}
$major = 0
if (-not [int]::TryParse(($version -split '\.')[0], [ref]$major) -or $major -lt $MinimumMajor) {
throw "Node.js $MinimumMajor or newer is required; found $version at $runtimePath."
}
return [pscustomobject]@{ Path = $runtimePath; Version = $version; Major = $major }
}
function ConvertTo-DreamSkinCodexInstall {
param(
[Parameter(Mandatory = $true)][object]$Package,
[AllowNull()][object]$Manifest
)
if ("$($Package.Name)" -ine 'OpenAI.Codex' -or -not $Package.InstallLocation -or
-not $Package.PackageFullName -or -not $Package.PackageFamilyName -or
"$($Package.SignatureKind)" -ine 'Store' -or [bool]$Package.IsDevelopmentMode) {
return $null
}
$packageRoot = "$($Package.InstallLocation)"
$executable = Join-Path $packageRoot 'app\ChatGPT.exe'
if (-not (Test-Path -LiteralPath $executable)) { return $null }
try {
if (-not $PSBoundParameters.ContainsKey('Manifest')) {
$Manifest = Get-AppxPackageManifest -Package $Package -ErrorAction Stop
}
$applications = @($Manifest.Package.Applications.Application | Where-Object {
"$($_.Executable)".Replace('/', '\') -ieq 'app\ChatGPT.exe'
})
if ($applications.Count -ne 1) { return $null }
$applicationId = "$($applications[0].Id)"
} catch {
return $null
}
$packageFamilyName = "$($Package.PackageFamilyName)"
if ($packageFamilyName -cnotmatch '^[A-Za-z0-9._-]{1,128}$' -or
$applicationId -cnotmatch '^[A-Za-z0-9._-]{1,64}$') {
return $null
}
return [pscustomobject]@{
PackageRoot = $packageRoot
Executable = $executable
Version = "$($Package.Version)"
PackageFullName = "$($Package.PackageFullName)"
PackageFamilyName = $packageFamilyName
ApplicationId = $applicationId
AppUserModelId = "$packageFamilyName!$applicationId"
SignatureKind = "$($Package.SignatureKind)"
}
}
function Get-DreamSkinRegisteredCodexInstalls {
$packages = @(Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction Stop | Sort-Object Version -Descending)
$installs = @()
foreach ($package in $packages) {
$install = ConvertTo-DreamSkinCodexInstall -Package $package
if ($null -ne $install) { $installs += $install }
}
return $installs
}
function Get-DreamSkinCodexInstall {
$installs = @(Get-DreamSkinRegisteredCodexInstalls)
if ($installs.Count -eq 0) { throw 'The official OpenAI.Codex Store package is not installed or its identity cannot be validated.' }
return $installs[0]
}
function Initialize-DreamSkinPackageLauncher {
if ('CodexDreamSkin.PackageLauncher' -as [type]) { return }
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
namespace CodexDreamSkin {
[Flags]
internal enum ActivateOptions : uint {
None = 0
}
[ComImport]
[Guid("2e941141-7f97-4756-ba1d-9decde894a3d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IApplicationActivationManager {
[PreserveSig]
int ActivateApplication(
[MarshalAs(UnmanagedType.LPWStr)] string appUserModelId,
[MarshalAs(UnmanagedType.LPWStr)] string arguments,
ActivateOptions options,
out uint processId);
}
[ComImport]
[Guid("45ba127d-10a8-46ea-8ab7-56ea9078943c")]
internal class ApplicationActivationManager {}
public static class PackageLauncher {
public static uint Launch(string appUserModelId, string arguments) {
var manager = (IApplicationActivationManager)new ApplicationActivationManager();
try {
uint processId;
int result = manager.ActivateApplication(
appUserModelId,
arguments ?? string.Empty,
ActivateOptions.None,
out processId);
Marshal.ThrowExceptionForHR(result);
return processId;
} finally {
if (Marshal.IsComObject(manager)) Marshal.FinalReleaseComObject(manager);
}
}
}
}
'@
}
function Start-DreamSkinCodex {
param(
[Parameter(Mandatory = $true)][object]$Codex,
[AllowEmptyCollection()][string[]]$Arguments = @()
)
$appUserModelId = "$($Codex.AppUserModelId)"
if ($appUserModelId -cnotmatch '^[A-Za-z0-9._-]{1,128}![A-Za-z0-9._-]{1,64}$') {
throw 'The registered Codex AppUserModelId is unavailable or invalid.'
}
Initialize-DreamSkinPackageLauncher
$argumentLine = ConvertTo-DreamSkinArgumentLine -Arguments $Arguments
$processId = [CodexDreamSkin.PackageLauncher]::Launch($appUserModelId, $argumentLine)
if ($processId -le 0) { throw 'Windows did not return a Codex process ID after package activation.' }
return $processId
}
function Get-DreamSkinCodexStatePathCandidate {
param([AllowNull()][object]$State)
if ($null -eq $State -or -not $State.codexExe -or -not $State.codexPackageRoot) { return $null }
$executable = "$($State.codexExe)"
$packageRoot = "$($State.codexPackageRoot)"
$expectedExecutable = Join-Path $packageRoot 'app\ChatGPT.exe'
if (-not (Test-DreamSkinPathEqual -Left $executable -Right $expectedExecutable)) { return $null }
return [pscustomobject]@{
PackageRoot = $packageRoot
Executable = $executable
Version = "$($State.codexVersion)"
FromState = $true
RegisteredPackageVerified = $false
}
}
function Resolve-DreamSkinCodexInstallFromState {
param(
[AllowNull()][object]$State,
[Parameter(Mandatory = $true)][AllowEmptyCollection()][object[]]$RegisteredInstalls
)
$candidate = Get-DreamSkinCodexStatePathCandidate -State $State
if ($null -eq $candidate) { return $null }
$hasFullName = [bool]$State.codexPackageFullName
$hasFamilyName = [bool]$State.codexPackageFamilyName
if ($hasFullName -xor $hasFamilyName) { return $null }
foreach ($install in $RegisteredInstalls) {
$pathMatches = (Test-DreamSkinPathEqual -Left $candidate.PackageRoot -Right $install.PackageRoot) -and
(Test-DreamSkinPathEqual -Left $candidate.Executable -Right $install.Executable)
if (-not $pathMatches) { continue }
if ($hasFullName -and ("$($State.codexPackageFullName)" -ine $install.PackageFullName -or
"$($State.codexPackageFamilyName)" -ine $install.PackageFamilyName)) {
continue
}
return [pscustomobject]@{
PackageRoot = $install.PackageRoot
Executable = $install.Executable
Version = $install.Version
PackageFullName = $install.PackageFullName
PackageFamilyName = $install.PackageFamilyName
ApplicationId = $install.ApplicationId
AppUserModelId = $install.AppUserModelId
SignatureKind = $install.SignatureKind
FromState = $true
RegisteredPackageVerified = $true
}
}
return $null
}
function Get-DreamSkinCodexInstallFromState {
param([AllowNull()][object]$State)
try { $installs = @(Get-DreamSkinRegisteredCodexInstalls) } catch { return $null }
return Resolve-DreamSkinCodexInstallFromState -State $State -RegisteredInstalls $installs
}
function Test-DreamSkinWebSocketUrl {
param([string]$Value, [int]$Port)
try {
$uri = [Uri]$Value
$hostName = $uri.Host.ToLowerInvariant()
return ($uri.IsAbsoluteUri -and $uri.Scheme -eq 'ws' -and $uri.Port -eq $Port -and
$hostName -in @('127.0.0.1', 'localhost', '::1', '[::1]') -and -not $uri.UserInfo -and
-not $uri.Query -and -not $uri.Fragment -and
$uri.AbsolutePath -cmatch '^/devtools/(?:page|browser)/[A-Za-z0-9._-]{1,200}$')
} catch {
return $false
}
}
function Test-DreamSkinCdpPageTarget {
param([AllowNull()][object]$Target, [int]$Port)
if ($null -eq $Target -or "$($Target.type)" -cne 'page' -or
"$($Target.url)" -notlike 'app://*') {
return $false
}
if ($Target.id -isnot [string]) { return $false }
$targetId = "$($Target.id)"
$webSocketUrl = "$($Target.webSocketDebuggerUrl)"
if (-not (Test-DreamSkinBrowserId -Value $targetId) -or
-not (Test-DreamSkinWebSocketUrl -Value $webSocketUrl -Port $Port)) {
return $false
}
try {
return ([Uri]$webSocketUrl).AbsolutePath -ceq "/devtools/page/$targetId"
} catch {
return $false
}
}
function Get-DreamSkinCdpTargets {
param([int]$Port)
try {
$targets = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/json/list" -TimeoutSec 2 `
-MaximumRedirection 0 -ErrorAction Stop
return @($targets | Where-Object { Test-DreamSkinCdpPageTarget -Target $_ -Port $Port })
} catch {
return @()
}
}
function Test-DreamSkinBrowserId {
param([string]$Value)
return [bool]($Value -and $Value.Length -le 200 -and $Value -cmatch '^[A-Za-z0-9._-]+$')
}
function Get-DreamSkinCdpBrowserIdentity {
param([int]$Port)
try {
$version = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/json/version" -TimeoutSec 2 `
-MaximumRedirection 0 -ErrorAction Stop
$webSocketUrl = "$($version.webSocketDebuggerUrl)"
if (-not (Test-DreamSkinWebSocketUrl -Value $webSocketUrl -Port $Port)) { return $null }
$uri = [Uri]$webSocketUrl
$match = [regex]::Match($uri.AbsolutePath, '^/devtools/browser/(?<id>[A-Za-z0-9._-]{1,200})$')
if (-not $match.Success -or $uri.Query -or $uri.Fragment) { return $null }
$browserId = $match.Groups['id'].Value
if (-not (Test-DreamSkinBrowserId -Value $browserId)) { return $null }
return [pscustomobject]@{
BrowserId = $browserId
WebSocketDebuggerUrl = $webSocketUrl
Browser = "$($version.Browser)"
}
} catch {
return $null
}
}
function Get-DreamSkinPortListeners {
param([int]$Port)
if (-not (Get-Command Get-NetTCPConnection -ErrorAction SilentlyContinue)) {
throw 'Get-NetTCPConnection is required to verify CDP listener ownership.'
}
return @(Get-NetTCPConnection -State Listen -LocalPort $Port -ErrorAction SilentlyContinue)
}
function Test-DreamSkinPortAvailable {
param([int]$Port)
return (Get-DreamSkinPortListeners -Port $Port).Count -eq 0
}
function Test-DreamSkinCodexPortOwner {
param([int]$Port, [Parameter(Mandatory = $true)][object]$Codex)
$listeners = Get-DreamSkinPortListeners -Port $Port
if ($listeners.Count -eq 0) { return $false }
foreach ($listener in $listeners) {
if ($listener.LocalAddress -notin @('127.0.0.1', '::1')) { return $false }
$process = Get-CimInstance Win32_Process -Filter "ProcessId = $([int]$listener.OwningProcess)" -ErrorAction SilentlyContinue
$processPath = if ($process) { Get-DreamSkinProcessExecutablePath -ProcessInfo $process } else { $null }
if (-not $processPath -or -not (Test-DreamSkinPathEqual -Left $processPath -Right $Codex.Executable)) {
return $false
}
}
return $true
}
function Get-DreamSkinVerifiedCdpIdentity {
param([int]$Port, [Parameter(Mandatory = $true)][object]$Codex)
if (-not (Test-DreamSkinCodexPortOwner -Port $Port -Codex $Codex)) { return $null }
$browser = Get-DreamSkinCdpBrowserIdentity -Port $Port
if ($null -eq $browser) { return $null }
$targets = Get-DreamSkinCdpTargets -Port $Port
if ($targets.Count -eq 0) { return $null }
if (-not (Test-DreamSkinCodexPortOwner -Port $Port -Codex $Codex)) { return $null }
return [pscustomobject]@{
BrowserId = $browser.BrowserId
BrowserWebSocketDebuggerUrl = $browser.WebSocketDebuggerUrl
Browser = $browser.Browser
TargetCount = $targets.Count
}
}
function Test-DreamSkinCodexCdpEndpoint {
param([int]$Port, [Parameter(Mandatory = $true)][object]$Codex)
return $null -ne (Get-DreamSkinVerifiedCdpIdentity -Port $Port -Codex $Codex)
}
function Select-DreamSkinPort {
param([int]$PreferredPort)
for ($candidate = $PreferredPort; $candidate -le [Math]::Min(65535, $PreferredPort + 100); $candidate++) {
if (Test-DreamSkinPortAvailable -Port $candidate) { return $candidate }
}
throw "No free loopback port was found between $PreferredPort and $([Math]::Min(65535, $PreferredPort + 100))."
}
function Wait-DreamSkinPortAvailable {
param([int]$Port, [int]$TimeoutSeconds = 5)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
if (Test-DreamSkinPortAvailable -Port $Port) { return $true }
Start-Sleep -Milliseconds 200
} while ((Get-Date) -lt $deadline)
return $false
}
function Read-DreamSkinState {
param([Parameter(Mandatory = $true)][string]$Path)
if (-not (Test-Path -LiteralPath $Path)) { return $null }
try {
$state = (Read-DreamSkinUtf8File -Path $Path) | ConvertFrom-Json -ErrorAction Stop
if ($null -eq $state -or $state -is [string] -or $state -is [array]) { throw 'State root must be an object.' }
$properties = @($state.PSObject.Properties.Name)
if ($properties -contains 'platform' -and "$($state.platform)" -ine 'windows') {
throw 'State platform is not Windows.'
}
$schemaVersion = 1
if ($properties -contains 'schemaVersion') {
$schemaVersion = 0
if (-not [int]::TryParse("$($state.schemaVersion)", [ref]$schemaVersion) -or
$schemaVersion -lt 1 -or $schemaVersion -gt 3) {
throw 'State schema is not supported.'
}
}
if ($schemaVersion -ge 3) {
foreach ($required in @(
'platform', 'port', 'injectorPid', 'injectorStartedAt', 'injectorPath', 'nodePath',
'codexExe', 'codexPackageRoot', 'codexPackageFullName', 'codexPackageFamilyName', 'browserId'
)) {
if ($properties -notcontains $required -or -not $state.$required) {
throw "State schema 3 is missing required field: $required"
}
}
}
if ($properties -contains 'port') {
$statePort = 0
if (-not [int]::TryParse("$($state.port)", [ref]$statePort)) { throw 'State port is invalid.' }
Assert-DreamSkinPort -Port $statePort
}
if ($properties -contains 'injectorPid' -and $null -ne $state.injectorPid) {
$statePid = 0
if (-not [int]::TryParse("$($state.injectorPid)", [ref]$statePid) -or $statePid -le 0) {
throw 'State injector PID is invalid.'
}
}
if ($properties -contains 'browserId' -and $state.browserId -and
-not (Test-DreamSkinBrowserId -Value "$($state.browserId)")) {
throw 'State browser ID is invalid.'
}
return $state
} catch {
throw "Dream Skin state is unreadable; it was preserved for inspection: $Path"
}
}
function Write-DreamSkinState {
param([Parameter(Mandatory = $true)][string]$Path, [Parameter(Mandatory = $true)][object]$State)
$json = $State | ConvertTo-Json -Depth 6
Write-DreamSkinUtf8FileAtomically -Path $Path -Content ($json + "`r`n")
}
function Archive-DreamSkinStateFile {
param([Parameter(Mandatory = $true)][string]$Path)
if (-not (Test-Path -LiteralPath $Path)) { return $null }
$directory = [System.IO.Path]::GetDirectoryName([System.IO.Path]::GetFullPath($Path))
$stamp = (Get-Date).ToString('yyyyMMdd-HHmmss-fff')
$archivePath = Join-Path $directory "state.stale-$stamp-$([guid]::NewGuid().ToString('N')).json"
Move-Item -LiteralPath $Path -Destination $archivePath -ErrorAction Stop
return $archivePath
}
function Get-DreamSkinProcessStartedAt {
param([int]$ProcessId)
try {
return (Get-Process -Id $ProcessId -ErrorAction Stop).StartTime.ToUniversalTime().ToString('o')
} catch {
return $null
}
}
function Stop-DreamSkinRecordedInjector {
param([AllowNull()][object]$State)
if ($null -eq $State -or -not $State.injectorPid) { return $true }
$processId = [int]$State.injectorPid
$process = Get-CimInstance Win32_Process -Filter "ProcessId = $processId" -ErrorAction SilentlyContinue
if (-not $process) { return $true }
$expectedInjector = if ($State.injectorPath) {
"$($State.injectorPath)"
} elseif ($State.skillRoot) {
Join-Path "$($State.skillRoot)" 'scripts\injector.mjs'
} else {
$null
}
$processPath = Get-DreamSkinProcessExecutablePath -ProcessInfo $process
$commandLine = "$($process.CommandLine)"
if (-not $processPath -or -not $commandLine) {
throw "The recorded injector PID $processId is running, but its identity cannot be inspected. State was preserved."
}
$isNodeExecutable = [System.IO.Path]::GetFileName("$processPath") -ieq 'node.exe'
$nodeMatches = -not $State.nodePath -or
(Test-DreamSkinPathEqual -Left $processPath -Right "$($State.nodePath)")
$injectorMatches = [bool]($expectedInjector -and
(Test-DreamSkinCommandLineToken -CommandLine $commandLine -Token $expectedInjector) -and
(Test-DreamSkinCommandLineToken -CommandLine $commandLine -Token '--watch'))
if ($State.port) {
$portPattern = '(?i)(?:^|\s)--port(?:=|\s+)' + [regex]::Escape("$($State.port)") + '(?=$|\s)'
$injectorMatches = $injectorMatches -and [regex]::IsMatch($commandLine, $portPattern)
} else {
$injectorMatches = $false
}
if ($State.browserId) {
$browserPattern = '(?:^|\s)(?i:--browser-id)(?:=|\s+)' + [regex]::Escape("$($State.browserId)") + '(?=$|\s)'
$injectorMatches = $injectorMatches -and [regex]::IsMatch($commandLine, $browserPattern)
}
$startedAt = Get-DreamSkinProcessStartedAt -ProcessId $processId
$startMatches = -not $State.injectorStartedAt -or $startedAt -eq "$($State.injectorStartedAt)"
$identityMatches = [bool]($isNodeExecutable -and $nodeMatches -and $injectorMatches -and $startMatches)
if (-not $identityMatches) {
throw "The recorded injector PID $processId is running, but its visible identity does not match the saved Dream Skin process. State was preserved."
}
Stop-Process -Id $processId -Force -ErrorAction Stop
try { Wait-Process -Id $processId -Timeout 5 -ErrorAction Stop } catch {}
if (Get-Process -Id $processId -ErrorAction SilentlyContinue) {
throw "The recorded Dream Skin injector did not stop: PID $processId"
}
return $true
}
function Get-DreamSkinCodexProcesses {
param([Parameter(Mandatory = $true)][object]$Codex)
return @(Get-CimInstance Win32_Process -Filter "Name = 'ChatGPT.exe'" -ErrorAction SilentlyContinue |
Where-Object {
$processPath = Get-DreamSkinProcessExecutablePath -ProcessInfo $_
Test-DreamSkinPathEqual -Left $processPath -Right $Codex.Executable
})
}
function Stop-DreamSkinCodex {
param([Parameter(Mandatory = $true)][object]$Codex, [switch]$AllowForce)
$processes = Get-DreamSkinCodexProcesses -Codex $Codex
if ($processes.Count -eq 0) { return }
foreach ($item in $processes) {
try { [void](Get-Process -Id $item.ProcessId -ErrorAction Stop).CloseMainWindow() } catch {}
}
$deadline = (Get-Date).AddSeconds(15)
while ((Get-DreamSkinCodexProcesses -Codex $Codex).Count -gt 0 -and (Get-Date) -lt $deadline) {
Start-Sleep -Milliseconds 250
}
$remaining = Get-DreamSkinCodexProcesses -Codex $Codex
if ($remaining.Count -eq 0) { return }
if (-not $AllowForce) {
throw 'Codex did not close within 15 seconds. Close it manually or explicitly authorize a forced restart.'
}
foreach ($item in $remaining) {
$current = Get-CimInstance Win32_Process -Filter "ProcessId = $([int]$item.ProcessId)" -ErrorAction SilentlyContinue
$currentPath = if ($current) { Get-DreamSkinProcessExecutablePath -ProcessInfo $current } else { $null }
if ($currentPath -and (Test-DreamSkinPathEqual -Left $currentPath -Right $Codex.Executable)) {
Stop-Process -Id $item.ProcessId -Force -ErrorAction SilentlyContinue
}
}
Start-Sleep -Milliseconds 500
if ((Get-DreamSkinCodexProcesses -Codex $Codex).Count -gt 0) { throw 'Codex could not be stopped safely.' }
}
function Confirm-DreamSkinRestart {
param([string]$Message)
$shell = New-Object -ComObject WScript.Shell
return $shell.Popup($Message, 0, 'Codex Dream Skin', 52) -eq 6
}