Repository navigation
Expand file tree
/
Copy pathstart.ps1
More file actions
138 lines (129 loc) · 7.33 KB
/
Copy pathstart.ps1
File metadata and controls
138 lines (129 loc) · 7.33 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
#Requires -Version 5.1
[CmdletBinding()]
# Assigned in the body, not here: an advanced script ([CmdletBinding()]) evaluates
# parameter defaults in the caller's scope, where $PSScriptRoot is empty.
param(
[string]$DataRoot = '',
[switch]$PrepareOnly
)
$ErrorActionPreference = 'Stop'
$repo = [IO.Path]::GetFullPath($PSScriptRoot)
$plugin = Join-Path $repo 'ds-bot'
$linkDir = Join-Path $repo 'link'
$appDir = Join-Path $repo 'app'
if (-not $DataRoot) { $DataRoot = '.ds-bot-local' }
if (-not [IO.Path]::IsPathRooted($DataRoot)) { $DataRoot = Join-Path $repo $DataRoot }
$DataRoot = [IO.Path]::GetFullPath($DataRoot)
$node = (Get-Command node -ErrorAction Stop).Source
$npm = (Get-Command npm.cmd -ErrorAction Stop).Source
function Invoke-Npm {
param([string]$Directory, [string[]]$Arguments)
Push-Location $Directory
try {
& $npm @Arguments
if ($LASTEXITCODE -ne 0) { throw "npm $($Arguments -join ' ') failed (exit $LASTEXITCODE)." }
} finally { Pop-Location }
}
function Get-SourceHash {
param([string[]]$Paths)
[string[]]$normalized = @($Paths | ForEach-Object { [IO.Path]::GetFullPath($_).Replace('/', '\').ToLowerInvariant() })
[Array]::Sort($normalized, [StringComparer]::Ordinal)
$lines = foreach ($path in $normalized) {
"$path $((Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash)"
}
$sha = [Security.Cryptography.SHA256]::Create()
try {
[BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes(($lines -join "`n")))).Replace('-', '')
} finally { $sha.Dispose() }
}
function Install-IfChanged {
param([string]$Directory, [string]$Hash, [string]$RequiredFile)
$stamp = Join-Path $Directory 'node_modules/.source-launcher.sha256'
if ((Test-Path -LiteralPath $RequiredFile) -and (Test-Path -LiteralPath $stamp) -and ([IO.File]::ReadAllText($stamp).Trim() -eq $Hash)) {
Write-Host "Dependencies ready: $(Split-Path $Directory -Leaf)"
return
}
Write-Host "Installing dependencies: $(Split-Path $Directory -Leaf)"
Invoke-Npm $Directory @('ci', '--include=dev', '--no-fund', '--no-audit')
if (-not (Test-Path -LiteralPath $RequiredFile)) { throw "Installation is incomplete: $RequiredFile" }
[IO.File]::WriteAllText($stamp, $Hash)
}
function Ensure-MediumIntegrity {
param([string]$Path)
$icacls = Get-Command icacls.exe -ErrorAction SilentlyContinue
if (-not $icacls) { return }
# Workspaces labeled Low Mandatory Level pass that label onto extracted binaries and DLLs;
# Windows then starts electron.exe at Low Integrity (failing user32.dll delay-load with
# ERROR_DLL_INIT_FAILED) and blocks sandboxed renderer/GPU processes from loading sibling
# DLLs such as ffmpeg.dll with ERROR_ACCESS_DENIED (exit -2147483645 / 0x80000003).
# Windows PowerShell 5.1 wraps native stderr merged into the success stream
# as a terminating ErrorRecord when $ErrorActionPreference is Stop, so keep
# this local and swallow every stream instead of aborting on icacls noise.
$local:ErrorActionPreference = 'Continue'
try {
& $icacls.Source $Path /setintegritylevel 'Medium' /T /Q *> $null
if ($LASTEXITCODE -ne 0) {
Write-Warning "Could not set Medium integrity level on $Path; Electron may fail to start under Low Mandatory Level."
}
} catch {
Write-Warning "Could not set Medium integrity level on $Path; Electron may fail to start under Low Mandatory Level."
}
}
$envNames = @('DS_BOT_HOME', 'DS_BOT_WORKSPACE', 'DS_BOT_SHELL_DATA', 'ELECTRON_RUN_AS_NODE')
$savedEnv = @{}
foreach ($name in $envNames) { $savedEnv[$name] = [Environment]::GetEnvironmentVariable($name, 'Process') }
try {
& $node --version
if ($LASTEXITCODE -ne 0) { throw 'Node.js could not start.' }
$pluginHash = Get-SourceHash @((Join-Path $plugin 'package.json'), (Join-Path $plugin 'package-lock.json'), (Join-Path $plugin '.npmrc'))
Install-IfChanged $plugin $pluginHash (Join-Path $plugin 'node_modules/esbuild/package.json')
$linkHash = Get-SourceHash @((Join-Path $linkDir 'package.json'), (Join-Path $linkDir 'package-lock.json'))
Install-IfChanged $linkDir $linkHash (Join-Path $linkDir 'node_modules/@noble/curves/package.json')
Write-Host 'Building the plugin from this checkout...'
Invoke-Npm $plugin @('run', 'build')
# The desktop app installs a copy of the plugin. Hash npm's package file list so new
# or removed source files also refresh that copy, even when its version is unchanged.
$packJson = Invoke-Npm $plugin @('pack', '--dry-run', '--json', '--ignore-scripts')
$package = @((($packJson -join "`n") | ConvertFrom-Json))[0]
if (-not $package.files) { throw 'npm did not return the plugin package file list.' }
$appFiles = @((Join-Path $appDir 'package.json'), (Join-Path $appDir 'package-lock.json'), (Join-Path $appDir '.npmrc'))
$appFiles += @($package.files | ForEach-Object { Join-Path $plugin $_.path })
$appHash = Get-SourceHash $appFiles
Install-IfChanged $appDir $appHash (Join-Path $appDir 'node_modules/electron/package.json')
$electronDist = Join-Path $appDir 'node_modules/electron/dist'
$electron = Join-Path $electronDist 'electron.exe'
$electronPathTxt = Join-Path $appDir 'node_modules/electron/path.txt'
$electronVersionFile = Join-Path $electronDist 'version'
$electronPkg = Join-Path $appDir 'node_modules/electron/package.json'
$electronVersionOk = $false
if ((Test-Path -LiteralPath $electron) -and (Test-Path -LiteralPath $electronPathTxt) -and (Test-Path -LiteralPath $electronVersionFile) -and (Test-Path -LiteralPath $electronPkg)) {
$wanted = (Get-Content -LiteralPath $electronPkg -Raw | ConvertFrom-Json).version
$have = (Get-Content -LiteralPath $electronVersionFile -Raw).Trim() -replace '^v', ''
$electronVersionOk = ($wanted -and $have -eq $wanted)
}
if (-not $electronVersionOk) {
if (Test-Path -LiteralPath $electronDist) { Remove-Item -LiteralPath $electronDist -Recurse -Force -ErrorAction SilentlyContinue }
if (Test-Path -LiteralPath $electronPathTxt) { Remove-Item -LiteralPath $electronPathTxt -Force -ErrorAction SilentlyContinue }
Write-Host 'Downloading the Electron runtime...'
& $node (Join-Path $appDir 'node_modules/electron/install.js')
if ($LASTEXITCODE -ne 0) { throw 'Electron download failed.' }
if (-not (Test-Path -LiteralPath $electron)) { throw 'Electron executable is still missing.' }
}
Ensure-MediumIntegrity $electronDist
$env:DS_BOT_HOME = Join-Path $DataRoot 'data'
$env:DS_BOT_WORKSPACE = Join-Path $DataRoot 'workspace'
$env:DS_BOT_SHELL_DATA = Join-Path $DataRoot 'shell'
Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue
foreach ($path in @($env:DS_BOT_HOME, $env:DS_BOT_WORKSPACE, $env:DS_BOT_SHELL_DATA)) {
[void][IO.Directory]::CreateDirectory($path)
}
Write-Host "Persistent data: $DataRoot"
if ($PrepareOnly) { Write-Host 'Ready. Run start.ps1 to open DS Bot.'; return }
Write-Host 'Starting DS Bot. Configure model providers in Settings; close the window to stop.'
Invoke-Npm $appDir @('start')
} finally {
foreach ($name in $envNames) {
if ($null -eq $savedEnv[$name]) { Remove-Item "Env:$name" -ErrorAction SilentlyContinue }
else { [Environment]::SetEnvironmentVariable($name, $savedEnv[$name], 'Process') }
}
}