Something went wrong. Try again.
Open source agent hosting on the AT Protocol
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196# Installs the languid CLI on Windows, or updates it: irm https://languid.ai/install.ps1 | iex## Needs Node 22.18 or newer, and Git for Windows for the bash your agents run commands in. The code# goes in ~\.languid\app and a `languid` command in ~\.local\bin, which joins your PATH. Running it# again updates both and restarts the Languid tray. Then, on a first install, it signs you in# (LANGUID_NO_LOGIN=1 skips that).## The service fills in @SERVICE@ and @TARBALL@ when it serves this file.$ErrorActionPreference = 'Stop'$ProgressPreference = 'SilentlyContinue'
function Fail($msg) { throw "languid install: $msg" }
# MARK: Problem reports. When a step fails, the service's ops log gets the step, the error and the# Windows, PowerShell and Node versions (ai.languid.ops.report; LANGUID_NO_REPORTS=1 turns it off).# The installer's output and a note go too only if the person says yes.$script:LanguidStep = 'check'$script:LanguidOutput = New-Object System.Collections.Generic.List[string]function Say([string]$msg, [string]$color) { $script:LanguidOutput.Add($msg) if ($color) { Write-Host $msg -ForegroundColor $color } else { Write-Host $msg }}function Hide-Private([string]$s) { if (-not $s) { return $s } if ($HOME) { $s = $s.Replace($HOME, '~') } if ($env:USERNAME -and $env:USERNAME.Length -ge 3) { $s = $s -replace [regex]::Escape($env:USERNAME), '<user>' } $s}function Post-LanguidReport($body) { try { $json = $body | ConvertTo-Json -Compress $r = Invoke-RestMethod -Method Post -Uri "$Service/xrpc/ai.languid.ops.report" -ContentType 'application/json; charset=utf-8' -Body ([Text.Encoding]::UTF8.GetBytes($json)) -TimeoutSec 10 -UseBasicParsing return $r.id } catch { return $null }}function Send-LanguidReport($err, [string]$step, [string]$level = 'error') { if ($env:LANGUID_NO_REPORTS) { return } $msg = if ($err -is [System.Management.Automation.ErrorRecord]) { $err.Exception.Message } else { "$err" } $nodeVersion = $(try { "$(& node --version 2>$null)" } catch { '' }) $hasBash = [bool](@("$env:ProgramFiles\Git\bin\bash.exe", "${env:ProgramFiles(x86)}\Git\bin\bash.exe") | Where-Object { $_ -and (Test-Path $_) }) $body = [ordered]@{ step = $step; level = $level; message = (Hide-Private $msg) platform = "win32/$env:PROCESSOR_ARCHITECTURE"; os = [Environment]::OSVersion.VersionString shell = "PowerShell $($PSVersionTable.PSVersion)$(if ($hasBash) { ', Git Bash' } else { ', no Git Bash' })" node = $(if ($nodeVersion) { "node $nodeVersion" } else { 'no node' }); version = "client-$ReleaseVersion" } if ($err -is [System.Management.Automation.ErrorRecord]) { $body['name'] = $err.Exception.GetType().FullName $body['stack'] = Hide-Private "$($err.ScriptStackTrace)" } $id = Post-LanguidReport $body if ($id) { Write-Host "Languid got a report of this error ($id): the step that failed, the error, and your Windows, PowerShell and Node versions." } # A missing Node or tar is fixed by installing it; asking for the output wouldn't help. if ($step -eq 'check' -or $env:LANGUID_NO_PROMPT -or -not [Environment]::UserInteractive) { return } try { $yes = (Read-Host "Send the installer's output and a note too, to help fix it? [y/N]") -match '^\s*y(es)?\s*$' if (-not $yes) { return } $note = Read-Host 'What were you doing? (Enter to skip)' } catch { return } $details = @($script:LanguidOutput) + @('', ($err | Out-String)) $more = [ordered]@{ step = $step; level = $level; message = $body.message; platform = $body.platform; os = $body.os; shell = $body.shell; node = $body.node; version = $body.version; log = (Hide-Private ($details -join "`n")) } if ($id) { $more['id'] = $id } if ($note) { $more['note'] = Hide-Private $note } $sent = Post-LanguidReport $more if ($sent) { Write-Host "Sent ($sent). Thanks." } else { Write-Host "Couldn't reach $Service to send it." -ForegroundColor Yellow }}
$Service = if ($env:LANGUID_SERVER) { $env:LANGUID_SERVER } else { '@SERVICE@' }$Tarball = if ($env:LANGUID_TARBALL) { $env:LANGUID_TARBALL } else { '@TARBALL@' }$ReleaseVersion = if ($env:LANGUID_RELEASE_VERSION) { $env:LANGUID_RELEASE_VERSION } else { '@VERSION@' }$ReleaseRevision = if ($env:LANGUID_RELEASE_REVISION) { $env:LANGUID_RELEASE_REVISION } else { '@REVISION@' }$ReleaseSha256 = if ($env:LANGUID_RELEASE_SHA256) { $env:LANGUID_RELEASE_SHA256 } else { '@SHA256@' }if ($ReleaseVersion -cnotmatch '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$' -or $ReleaseRevision -cnotmatch '^[a-f0-9]{40}$' -or $ReleaseSha256 -cnotmatch '^[a-f0-9]{64}$') { Fail 'missing or invalid versioned release metadata' }$App = if ($env:LANGUID_APP_DIR) { $env:LANGUID_APP_DIR } else { Join-Path $HOME '.languid\app' }$Bin = if ($env:LANGUID_BIN_DIR) { $env:LANGUID_BIN_DIR } else { Join-Path $HOME '.local\bin' }
# MARK: The install, as a function so a failing step can be reported (and nothing leaks into the session).function Install-Languid { if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Fail 'needs Node 22.18 or newer: https://nodejs.org' } if (-not (Get-Command npx -ErrorAction SilentlyContinue)) { Fail 'needs npx, which comes with Node: https://nodejs.org' } if (-not (Get-Command tar -ErrorAction SilentlyContinue)) { Fail 'needs tar, which comes with Windows 10 and later' } & node -e "const [a, b] = process.versions.node.split('.').map(Number); process.exit(a > 22 || (a === 22 && b >= 18) ? 0 : 1)" if ($LASTEXITCODE -ne 0) { Fail "needs Node 22.18 or newer (this is $(node --version)): https://nodejs.org" } $gitBash = @("$env:ProgramFiles\Git\bin\bash.exe", "${env:ProgramFiles(x86)}\Git\bin\bash.exe") | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1 if (-not $gitBash) { Say 'Your agents run commands in bash, which comes with Git for Windows: https://git-scm.com/download/win' Yellow }
$script:LanguidStep = 'download' New-Item -ItemType Directory -Force -Path (Split-Path $App), $Bin | Out-Null $New = "$App.new.$PID" $Old = "$App.old.$PID" $Tgz = "$New.tgz" $Committed = $false $Swapped = $false $LockPath = "$App.install.lock" try { $Lock = [IO.File]::Open($LockPath, 'CreateNew', 'Write', 'None') } catch { Fail "another installer is running (lock: $LockPath)" } try { Say 'downloading languid…' try { Invoke-WebRequest -Uri $Tarball -OutFile $Tgz -UseBasicParsing } catch { Fail "could not download ${Tarball}: $($_.Exception.Message)" } if ((Get-FileHash -Path $Tgz -Algorithm SHA256).Hash.ToLowerInvariant() -cne $ReleaseSha256) { Fail 'release checksum mismatch; nothing changed' } $script:LanguidStep = 'unpack' New-Item -ItemType Directory -Force -Path $New | Out-Null & tar -xzf $Tgz -C $New --strip-components=1 if ($LASTEXITCODE -ne 0) { Fail "could not unpack $Tarball" } if (-not (Test-Path (Join-Path $New 'src\cli.ts'))) { Fail "$Tarball doesn't look like languid" }
$script:LanguidStep = 'dependencies' # The tray holds files in the old copy open; stop it before the swap, and start it again after. $tray = Get-CimInstance Win32_Process -Filter "Name = 'powershell.exe'" | Where-Object { $_.CommandLine -like "*$App\windows\Languid.ps1*" } $tray | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
# Swap it in before installing: pnpm writes absolute paths into node_modules\.bin. $first = -not (Test-Path $App) if (-not $first) { Move-Item $App $Old; $Swapped = $true } Move-Item $New $App $Swapped = $true Say 'installing its dependencies…' Push-Location $App try { $pnpm = (& node -p "require('./package.json').packageManager.split('@')[1]") if ($LASTEXITCODE -ne 0) { Fail 'could not read the pinned package manager' } $env:NPM_CONFIG_UPDATE_NOTIFIER = 'false' # Its output goes in the installer's output, for a problem report; npx's warnings on stderr mustn't stop the script. $ErrorActionPreference = 'Continue' try { $out = & npx --yes "pnpm@$pnpm" install --frozen-lockfile --reporter=silent 2>&1 $ok = $LASTEXITCODE -eq 0 } finally { $ErrorActionPreference = 'Stop' } foreach ($line in @($out)) { if ("$line") { Say "$line" } } } finally { Pop-Location } if (-not $ok) { Fail 'installing dependencies failed; restoring the previous installation' } $script:LanguidStep = 'wrapper' @{ version=$ReleaseVersion; revision=$ReleaseRevision; sha256=$ReleaseSha256; service=$Service; binDir=[IO.Path]::GetFullPath($Bin) } | ConvertTo-Json | Set-Content -Path (Join-Path $App 'client-install.json') -Encoding UTF8 $Wrapper = Join-Path $Bin ".languid.new.$PID.cmd" Set-Content -Path $Wrapper -Encoding ASCII -Value "@echo off`r`nnode `"$App\src\cli.ts`" %*" Move-Item -Force $Wrapper (Join-Path $Bin 'languid.cmd') Copy-Item -Force (Join-Path $Bin 'languid.cmd') (Join-Path $Bin 'marshell.cmd') $Committed = $true if (Test-Path $Old) { Remove-Item -Recurse -Force $Old -ErrorAction SilentlyContinue } } finally { if ($Swapped -and -not $Committed) { Remove-Item -Recurse -Force $App -ErrorAction SilentlyContinue if (Test-Path $Old) { Move-Item $Old $App } if ($tray) { Start-Process -WindowStyle Hidden -FilePath powershell.exe -ArgumentList @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$App\windows\Languid.ps1`"") } } $Lock.Dispose() Remove-Item -Force $LockPath -ErrorAction SilentlyContinue Remove-Item -Force $Tgz -ErrorAction SilentlyContinue if (Test-Path $New) { Remove-Item -Recurse -Force $New -ErrorAction SilentlyContinue } }
$script:LanguidStep = 'path' Say "installed languid ${ReleaseVersion}: $Bin\languid.cmd (and $Bin\marshell.cmd)"
# A computer set up from this install keeps running the old code until the tray restarts it. if ($tray) { Start-Process -WindowStyle Hidden -FilePath powershell.exe -ArgumentList @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-WindowStyle', 'Hidden', '-File', "`"$App\windows\Languid.ps1`"") Say 'restarted the Languid tray' }
# Put ~\.local\bin on the user's PATH, once. $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (-not (($userPath -split ';') -contains $Bin)) { [Environment]::SetEnvironmentVariable('Path', ($(if ($userPath) { "$userPath;" } else { '' }) + $Bin), 'User') Say "added $Bin to your PATH" } if (-not (($env:Path -split ';') -contains $Bin)) { $env:Path = "$env:Path;$Bin" }
if (-not $first) { Write-Host 'updated' } elseif (-not $env:LANGUID_NO_LOGIN -and [Environment]::UserInteractive) { Write-Host '' $script:LanguidStep = 'login' & "$Bin\languid.cmd" login --url $Service $code = $LASTEXITCODE if ($code -eq 0) { Write-Host ''; Write-Host 'run: languid' } else { Write-Host '' Send-LanguidReport "languid login exited with code $code" 'login' 'warn' Write-Host 'sign in with: languid login' } } else { Write-Host '' Write-Host 'sign in with: languid login' }}
try { Install-Languid} catch { $m = $_.Exception.Message Write-Host $(if ($m -like 'languid install:*') { $m } else { "languid install: $m" }) -ForegroundColor Red Send-LanguidReport $_ $script:LanguidStep if (-not [Environment]::UserInteractive) { throw }}