@okxweb3/a2a-node 0.1.3 → 0.1.4-beta-520175e226-260702152522

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,339 @@
1
+ # okx-a2a Windows environment fixer (Windows)
2
+ #
3
+ # Usage:
4
+ # .\fix-windows-env.ps1
5
+ #
6
+ # Behavior:
7
+ # - Diagnoses node/npm/okx-a2a resolution and prints every finding.
8
+ # - Rewrites the okx-a2a shims in %USERPROFILE%\.local\bin so they delegate
9
+ # to the npm global shim in %APPDATA%\npm (same templates as install.ps1).
10
+ # - Ensures %USERPROFILE%\.local\bin and %APPDATA%\npm are on the user PATH.
11
+ # - Does NOT install or uninstall any package and does not touch any data.
12
+ # - Idempotent: safe to run multiple times. Run it BEFORE installing the
13
+ # latest @okxweb3/a2a-node.
14
+ # - A full transcript is appended to %USERPROFILE%\.okx-a2a-fix-windows-env.log.
15
+
16
+ $ErrorActionPreference = "Stop"
17
+
18
+ if (-not $env:USERPROFILE) {
19
+ throw "USERPROFILE is required to fix the okx-a2a environment on Windows."
20
+ }
21
+
22
+ $LOG_PATH = Join-Path $env:USERPROFILE ".okx-a2a-fix-windows-env.log"
23
+ $INSTALL_DIR = Join-Path $env:USERPROFILE ".local\bin"
24
+ $NPM_GLOBAL_BIN = if ($env:APPDATA) { Join-Path $env:APPDATA "npm" } else { $null }
25
+
26
+ $transcriptStarted = $false
27
+ try {
28
+ Start-Transcript -Path $LOG_PATH -Append | Out-Null
29
+ $transcriptStarted = $true
30
+ } catch {
31
+ Write-Host "[fix] WARNING: unable to start transcript at ${LOG_PATH}: $($_.Exception.Message)"
32
+ }
33
+
34
+ function Write-Fix {
35
+ param([string]$Message)
36
+
37
+ Write-Host "[fix] $Message"
38
+ }
39
+
40
+ function Resolve-ExternalCommand {
41
+ param([string[]]$Names)
42
+
43
+ foreach ($name in $Names) {
44
+ $command = Get-Command $name -ErrorAction SilentlyContinue
45
+ if ($command -and $command.Source) {
46
+ return $command.Source
47
+ }
48
+ }
49
+ return $null
50
+ }
51
+
52
+ function Get-LastNonEmptyLine {
53
+ param([object[]]$Lines)
54
+
55
+ $line = @($Lines) | Where-Object { "$_" -match '\S' } | Select-Object -Last 1
56
+ if ($null -eq $line) {
57
+ return $null
58
+ }
59
+ return "$line".Trim()
60
+ }
61
+
62
+ function Show-CommandSource {
63
+ param([string]$Name)
64
+
65
+ $command = Get-Command $Name -ErrorAction SilentlyContinue
66
+ if ($command -and $command.Source) {
67
+ Write-Fix "command ${Name}: $($command.Source)"
68
+ } elseif ($command) {
69
+ Write-Fix "command ${Name}: resolved as $($command.CommandType) without a file source"
70
+ } else {
71
+ Write-Fix "command ${Name}: not found"
72
+ }
73
+ }
74
+
75
+ function Get-GlobalPackageCliPath {
76
+ if (-not $NPM_CMD) {
77
+ Write-Fix "npm is not available; cannot locate the global @okxweb3/a2a-node CLI entry"
78
+ return $null
79
+ }
80
+
81
+ $npmRootOutput = $null
82
+ try {
83
+ $npmRootOutput = & $NPM_CMD root -g
84
+ } catch {
85
+ Write-Fix "npm root -g failed: $($_.Exception.Message)"
86
+ return $null
87
+ }
88
+ $npmRoot = Get-LastNonEmptyLine -Lines @($npmRootOutput)
89
+ if (-not $npmRoot) {
90
+ Write-Fix "unable to detect the npm global node_modules directory"
91
+ return $null
92
+ }
93
+
94
+ $cliPath = Join-Path (Join-Path (Join-Path $npmRoot "@okxweb3") "a2a-node") "dist\cli.js"
95
+ if (-not (Test-Path $cliPath)) {
96
+ Write-Fix "global @okxweb3/a2a-node CLI entry not found at $cliPath"
97
+ return $null
98
+ }
99
+ return $cliPath
100
+ }
101
+
102
+ function Test-LegacyCmdShimContent {
103
+ param([string]$Content)
104
+
105
+ if (-not $Content) {
106
+ return $false
107
+ }
108
+ # Historical shim formats either exported NODE_EXE= or embedded an absolute
109
+ # per-user path (which breaks for non-ASCII Windows usernames).
110
+ if ($Content -match "NODE_EXE=") {
111
+ return $true
112
+ }
113
+ if ($Content -match "[A-Za-z]:\\Users\\") {
114
+ return $true
115
+ }
116
+ return $false
117
+ }
118
+
119
+ function Compare-ShimContent {
120
+ param(
121
+ [string]$Existing,
122
+ [string]$Expected
123
+ )
124
+
125
+ if (-not $Existing) {
126
+ return $false
127
+ }
128
+ $normalizedExisting = ($Existing -replace "`r`n", "`n").Trim()
129
+ $normalizedExpected = ($Expected -replace "`r`n", "`n").Trim()
130
+ return $normalizedExisting -eq $normalizedExpected
131
+ }
132
+
133
+ function Add-DirectoryToUserPath {
134
+ param(
135
+ [string]$Directory,
136
+ [string]$Label
137
+ )
138
+
139
+ $userPath = [Environment]::GetEnvironmentVariable("Path", "User")
140
+ $entries = @()
141
+ if ($userPath) {
142
+ $entries = @($userPath -split ";" | Where-Object { $_ })
143
+ }
144
+ $directoryNormalized = $Directory.TrimEnd([char]'\')
145
+ $alreadyPresent = $entries | Where-Object { $_.TrimEnd([char]'\') -ieq $directoryNormalized } | Select-Object -First 1
146
+ if ($alreadyPresent) {
147
+ Write-Fix "$Label already on user PATH: $Directory"
148
+ $env:Path = "${Directory};${env:Path}"
149
+ return
150
+ }
151
+
152
+ $newPath = if ($userPath) { "${Directory};${userPath}" } else { $Directory }
153
+ [Environment]::SetEnvironmentVariable("Path", $newPath, "User")
154
+ $env:Path = "${Directory};${env:Path}"
155
+ Write-Fix "added $Label to user PATH: $Directory (restart the terminal to pick it up in other sessions)"
156
+ }
157
+
158
+ function Repair-LocalBinShims {
159
+ if (-not (Test-Path $INSTALL_DIR)) {
160
+ New-Item -ItemType Directory -Path $INSTALL_DIR -Force | Out-Null
161
+ Write-Fix "created $INSTALL_DIR"
162
+ }
163
+
164
+ $cmdPath = Join-Path $INSTALL_DIR "okx-a2a.cmd"
165
+ $ps1Path = Join-Path $INSTALL_DIR "okx-a2a.ps1"
166
+
167
+ # Keep this template byte-identical to Write-LocalBinShims in install.ps1.
168
+ # It delegates through %APPDATA%\npm so no absolute per-user path (e.g. a
169
+ # non-ASCII Windows username) is ever embedded into the shim.
170
+ $cmdContent = @"
171
+ @ECHO off
172
+ SETLOCAL
173
+ IF NOT DEFINED APPDATA (
174
+ ECHO APPDATA is required to find the npm okx-a2a shim. >&2
175
+ EXIT /b 1
176
+ )
177
+ SET "OKX_A2A_NPM_CMD=%APPDATA%\npm\okx-a2a.cmd"
178
+ IF NOT EXIST "%OKX_A2A_NPM_CMD%" (
179
+ ECHO okx-a2a npm shim not found at "%OKX_A2A_NPM_CMD%" >&2
180
+ EXIT /b 1
181
+ )
182
+ CALL "%OKX_A2A_NPM_CMD%" %*
183
+ EXIT /b %ERRORLEVEL%
184
+ "@
185
+
186
+ $existingCmd = $null
187
+ if (Test-Path $cmdPath) {
188
+ $existingCmd = Get-Content -Path $cmdPath -Raw -ErrorAction SilentlyContinue
189
+ }
190
+ if (Test-LegacyCmdShimContent -Content $existingCmd) {
191
+ Write-Fix "found legacy shim, rewriting $cmdPath"
192
+ Set-Content -Path $cmdPath -Value $cmdContent -Encoding ASCII
193
+ Write-Fix "rewrote cmd shim: $cmdPath"
194
+ } elseif (Compare-ShimContent -Existing $existingCmd -Expected $cmdContent) {
195
+ Write-Fix "cmd shim already up to date: $cmdPath"
196
+ } else {
197
+ Set-Content -Path $cmdPath -Value $cmdContent -Encoding ASCII
198
+ Write-Fix "rewrote cmd shim: $cmdPath"
199
+ }
200
+
201
+ if (-not $NODE_CMD) {
202
+ Write-Fix "node executable not found; skipped rewriting $ps1Path"
203
+ return
204
+ }
205
+ $cliPath = Get-GlobalPackageCliPath
206
+ if (-not $cliPath) {
207
+ Write-Fix "skipped rewriting $ps1Path (global CLI entry unavailable)"
208
+ return
209
+ }
210
+
211
+ # Keep this template byte-identical to Write-LocalBinShims in install.ps1.
212
+ $psContent = @"
213
+ `$node = '$($NODE_CMD.Replace("'", "''"))'
214
+ `$cli = '$($cliPath.Replace("'", "''"))'
215
+ if (-not (Test-Path `$cli)) {
216
+ Write-Error "okx-a2a CLI entry not found at `$cli"
217
+ exit 1
218
+ }
219
+ & `$node `$cli @args
220
+ exit `$LASTEXITCODE
221
+ "@
222
+
223
+ $existingPs = $null
224
+ if (Test-Path $ps1Path) {
225
+ $existingPs = Get-Content -Path $ps1Path -Raw -ErrorAction SilentlyContinue
226
+ }
227
+ if (Compare-ShimContent -Existing $existingPs -Expected $psContent) {
228
+ Write-Fix "ps1 shim already up to date: $ps1Path"
229
+ } else {
230
+ Set-Content -Path $ps1Path -Value $psContent -Encoding UTF8
231
+ Write-Fix "rewrote ps1 shim: $ps1Path"
232
+ }
233
+ }
234
+
235
+ $exitCode = 0
236
+ try {
237
+ Write-Fix "=== okx-a2a fix-windows-env started at $(Get-Date -Format o) ==="
238
+ Write-Fix "transcript: $LOG_PATH"
239
+ Write-Fix "PowerShell version: $($PSVersionTable.PSVersion)"
240
+ try {
241
+ Write-Fix "execution policy (effective): $(Get-ExecutionPolicy)"
242
+ Write-Fix "execution policy (CurrentUser): $(Get-ExecutionPolicy -Scope CurrentUser)"
243
+ } catch {
244
+ Write-Fix "unable to read execution policy: $($_.Exception.Message)"
245
+ }
246
+
247
+ # --- diagnostics ---
248
+ $NODE_CMD = Resolve-ExternalCommand -Names @("node.exe", "node")
249
+ if ($NODE_CMD) {
250
+ $nodeVersion = $null
251
+ try {
252
+ $nodeVersion = Get-LastNonEmptyLine -Lines @(& $NODE_CMD -v)
253
+ } catch {
254
+ Write-Fix "node -v failed: $($_.Exception.Message)"
255
+ }
256
+ Write-Fix "node: $NODE_CMD (version: $nodeVersion)"
257
+ } else {
258
+ Write-Fix "node: not found"
259
+ }
260
+
261
+ $NPM_CMD = Resolve-ExternalCommand -Names @("npm.cmd", "npm")
262
+ if ($NPM_CMD) {
263
+ $npmVersion = $null
264
+ try {
265
+ $npmVersion = Get-LastNonEmptyLine -Lines @(& $NPM_CMD -v)
266
+ } catch {
267
+ Write-Fix "npm -v failed: $($_.Exception.Message)"
268
+ }
269
+ Write-Fix "npm: $NPM_CMD (version: $npmVersion)"
270
+ try {
271
+ $npmPrefix = Get-LastNonEmptyLine -Lines @(& $NPM_CMD config get prefix)
272
+ Write-Fix "npm config get prefix: $npmPrefix"
273
+ } catch {
274
+ Write-Fix "npm config get prefix failed: $($_.Exception.Message)"
275
+ }
276
+ try {
277
+ $npmRoot = Get-LastNonEmptyLine -Lines @(& $NPM_CMD root -g)
278
+ Write-Fix "npm root -g: $npmRoot"
279
+ } catch {
280
+ Write-Fix "npm root -g failed: $($_.Exception.Message)"
281
+ }
282
+ } else {
283
+ Write-Fix "npm: not found"
284
+ }
285
+
286
+ if ($NPM_GLOBAL_BIN) {
287
+ if (Test-Path $NPM_GLOBAL_BIN) {
288
+ Write-Fix "APPDATA npm dir exists: $NPM_GLOBAL_BIN"
289
+ } else {
290
+ Write-Fix "APPDATA npm dir missing: $NPM_GLOBAL_BIN"
291
+ }
292
+ } else {
293
+ Write-Fix "APPDATA is not defined; cannot locate the npm global bin dir"
294
+ }
295
+
296
+ Show-CommandSource -Name "okx-a2a"
297
+ Show-CommandSource -Name "codex"
298
+ Show-CommandSource -Name "claude"
299
+
300
+ # --- repair: local bin shims ---
301
+ $npmShimPath = $null
302
+ if ($NPM_GLOBAL_BIN) {
303
+ $npmShimPath = Join-Path $NPM_GLOBAL_BIN "okx-a2a.cmd"
304
+ }
305
+ if ($npmShimPath -and (Test-Path $npmShimPath)) {
306
+ Write-Fix "npm global shim found: $npmShimPath"
307
+ Repair-LocalBinShims
308
+ } else {
309
+ Write-Fix "npm global okx-a2a shim not found (expected at $npmShimPath)"
310
+ Write-Fix "this script does not install packages; run 'npm install -g @okxweb3/a2a-node' or install.ps1 first, then re-run this script"
311
+ }
312
+
313
+ # --- repair: user PATH ---
314
+ Add-DirectoryToUserPath -Directory $INSTALL_DIR -Label "local bin"
315
+ if ($NPM_GLOBAL_BIN) {
316
+ Add-DirectoryToUserPath -Directory $NPM_GLOBAL_BIN -Label "npm global bin"
317
+ }
318
+
319
+ # --- verify ---
320
+ $resolved = Get-Command okx-a2a -ErrorAction SilentlyContinue
321
+ if ($resolved -and $resolved.Source) {
322
+ Write-Fix "okx-a2a now resolves to: $($resolved.Source)"
323
+ } else {
324
+ Write-Fix "okx-a2a is still not resolvable on PATH; see the messages above and the transcript at $LOG_PATH"
325
+ $exitCode = 1
326
+ }
327
+ Write-Fix "=== okx-a2a fix-windows-env finished (exit $exitCode) ==="
328
+ } catch {
329
+ Write-Fix "ERROR: $($_.Exception.Message)"
330
+ $exitCode = 1
331
+ } finally {
332
+ if ($transcriptStarted) {
333
+ try {
334
+ Stop-Transcript | Out-Null
335
+ } catch {}
336
+ }
337
+ }
338
+
339
+ exit $exitCode