@blunking/codexlink 0.1.12 → 0.1.14

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.
@@ -1,74 +1,74 @@
1
- param(
2
- [string]$Profile = "default",
3
- [switch]$EnsureConfigured,
4
- [switch]$Json
5
- )
6
-
7
- $ErrorActionPreference = "Stop"
8
-
9
- function Try-ReadJson {
10
- param([string]$Path)
11
- if (-not (Test-Path $Path)) { return $null }
12
- try { return Get-Content -Raw -Path $Path | ConvertFrom-Json } catch { return $null }
13
- }
14
-
15
- function Ensure-Dir {
16
- param([string]$Path)
17
- if (-not (Test-Path $Path)) {
18
- New-Item -ItemType Directory -Path $Path -Force | Out-Null
19
- }
20
- }
21
-
22
- function Resolve-ConfiguredPath {
23
- param([string]$Value, [string]$RuntimeRoot)
24
- if (-not $Value) { return "" }
25
- $expanded = [Environment]::ExpandEnvironmentVariables($Value)
26
- if ([System.IO.Path]::IsPathRooted($expanded)) { return $expanded }
27
- return [System.IO.Path]::GetFullPath((Join-Path $RuntimeRoot $expanded))
28
- }
29
-
30
- function Read-DotEnvFile {
31
- param([string]$Path)
32
- $values = @{}
33
- if (-not (Test-Path $Path)) { return $values }
34
- foreach ($line in (Get-Content -Path $Path)) {
35
- if (-not $line) { continue }
36
- if ($line.Trim().StartsWith("#")) { continue }
37
- $parts = $line -split "=", 2
38
- if ($parts.Count -ne 2) { continue }
39
- $values[$parts[0].Trim()] = $parts[1]
40
- }
41
- return $values
42
- }
43
-
44
- function Write-DotEnvFile {
45
- param(
46
- [string]$Path,
47
- [hashtable]$Values
48
- )
49
-
50
- $lines = foreach ($key in ($Values.Keys | Sort-Object)) {
51
- "$key=$($Values[$key])"
52
- }
53
- Set-Content -Path $Path -Value $lines -Encoding UTF8
54
- }
55
-
56
- function Test-TelegramTokenFormat {
57
- param([string]$Value)
58
- if (-not $Value) { return $false }
59
- return $Value -match '^\d{6,}:[A-Za-z0-9_-]{20,}$'
60
- }
61
-
1
+ param(
2
+ [string]$Profile = "default",
3
+ [switch]$EnsureConfigured,
4
+ [switch]$Json
5
+ )
6
+
7
+ $ErrorActionPreference = "Stop"
8
+
9
+ function Try-ReadJson {
10
+ param([string]$Path)
11
+ if (-not (Test-Path $Path)) { return $null }
12
+ try { return Get-Content -Raw -Path $Path | ConvertFrom-Json } catch { return $null }
13
+ }
14
+
15
+ function Ensure-Dir {
16
+ param([string]$Path)
17
+ if (-not (Test-Path $Path)) {
18
+ New-Item -ItemType Directory -Path $Path -Force | Out-Null
19
+ }
20
+ }
21
+
22
+ function Resolve-ConfiguredPath {
23
+ param([string]$Value, [string]$RuntimeRoot)
24
+ if (-not $Value) { return "" }
25
+ $expanded = [Environment]::ExpandEnvironmentVariables($Value)
26
+ if ([System.IO.Path]::IsPathRooted($expanded)) { return $expanded }
27
+ return [System.IO.Path]::GetFullPath((Join-Path $RuntimeRoot $expanded))
28
+ }
29
+
30
+ function Read-DotEnvFile {
31
+ param([string]$Path)
32
+ $values = @{}
33
+ if (-not (Test-Path $Path)) { return $values }
34
+ foreach ($line in (Get-Content -Path $Path)) {
35
+ if (-not $line) { continue }
36
+ if ($line.Trim().StartsWith("#")) { continue }
37
+ $parts = $line -split "=", 2
38
+ if ($parts.Count -ne 2) { continue }
39
+ $values[$parts[0].Trim()] = $parts[1]
40
+ }
41
+ return $values
42
+ }
43
+
44
+ function Write-DotEnvFile {
45
+ param(
46
+ [string]$Path,
47
+ [hashtable]$Values
48
+ )
49
+
50
+ $lines = foreach ($key in ($Values.Keys | Sort-Object)) {
51
+ "$key=$($Values[$key])"
52
+ }
53
+ Set-Content -Path $Path -Value $lines -Encoding UTF8
54
+ }
55
+
56
+ function Test-TelegramTokenFormat {
57
+ param([string]$Value)
58
+ if (-not $Value) { return $false }
59
+ return $Value -match '^\d{6,}:[A-Za-z0-9_-]{20,}$'
60
+ }
61
+
62
62
  function Test-AllowedChatIdsFormat {
63
- param([string]$Value)
64
- if (-not $Value) { return $false }
65
- $parts = @($Value -split "," | ForEach-Object { $_.Trim() } | Where-Object { $_ })
66
- if ($parts.Count -eq 0) { return $false }
67
- foreach ($part in $parts) {
68
- if ($part -notmatch '^-?\d+$') {
69
- return $false
70
- }
71
- }
63
+ param([string]$Value)
64
+ if (-not $Value) { return $false }
65
+ $parts = @($Value -split "," | ForEach-Object { $_.Trim() } | Where-Object { $_ })
66
+ if ($parts.Count -eq 0) { return $false }
67
+ foreach ($part in $parts) {
68
+ if ($part -notmatch '^-?\d+$') {
69
+ return $false
70
+ }
71
+ }
72
72
  return $true
73
73
  }
74
74
 
@@ -207,24 +207,24 @@ function Wait-TelegramPairingChat {
207
207
  }
208
208
 
209
209
  function Prompt-RequiredValue {
210
- param(
211
- [string]$Prompt,
212
- [string]$CurrentValue,
213
- [scriptblock]$Validator,
214
- [string]$ErrorMessage
215
- )
216
-
217
- while ($true) {
218
- $fullPrompt = if ($CurrentValue) { "$Prompt [$CurrentValue]" } else { $Prompt }
219
- $inputValue = Read-Host -Prompt $fullPrompt
220
- if (-not $inputValue -and $CurrentValue) {
221
- $inputValue = $CurrentValue
222
- }
223
- $inputValue = [string]$inputValue
224
- if (& $Validator $inputValue) {
225
- return $inputValue
226
- }
227
- Write-Host $ErrorMessage -ForegroundColor Yellow
210
+ param(
211
+ [string]$Prompt,
212
+ [string]$CurrentValue,
213
+ [scriptblock]$Validator,
214
+ [string]$ErrorMessage
215
+ )
216
+
217
+ while ($true) {
218
+ $fullPrompt = if ($CurrentValue) { "$Prompt [$CurrentValue]" } else { $Prompt }
219
+ $inputValue = Read-Host -Prompt $fullPrompt
220
+ if (-not $inputValue -and $CurrentValue) {
221
+ $inputValue = $CurrentValue
222
+ }
223
+ $inputValue = [string]$inputValue
224
+ if (& $Validator $inputValue) {
225
+ return $inputValue
226
+ }
227
+ Write-Host $ErrorMessage -ForegroundColor Yellow
228
228
  }
229
229
  }
230
230
 
@@ -256,19 +256,19 @@ function Get-ProfilePath {
256
256
  $runtimeRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
257
257
  $profilePath = Get-ProfilePath -RuntimeRoot $runtimeRoot -ProfileName $Profile
258
258
  $profileJson = Try-ReadJson -Path $profilePath
259
-
260
- if (-not $profileJson) {
261
- throw "Profile not found or invalid: $profilePath"
262
- }
263
-
264
- $profileAgent = if ($profileJson.agent_name) { [string]$profileJson.agent_name } else { $Profile.ToLower() }
265
- $stateDir = if ($profileJson.telegram -and $profileJson.telegram.state_dir) {
266
- Resolve-ConfiguredPath -Value ([string]$profileJson.telegram.state_dir) -RuntimeRoot $runtimeRoot
267
- } else {
268
- Join-Path $env:USERPROFILE (".codex\channels\telegram-" + $profileAgent)
269
- }
270
-
271
- Ensure-Dir -Path $stateDir
259
+
260
+ if (-not $profileJson) {
261
+ throw "Profile not found or invalid: $profilePath"
262
+ }
263
+
264
+ $profileAgent = if ($profileJson.agent_name) { [string]$profileJson.agent_name } else { $Profile.ToLower() }
265
+ $stateDir = if ($profileJson.telegram -and $profileJson.telegram.state_dir) {
266
+ Resolve-ConfiguredPath -Value ([string]$profileJson.telegram.state_dir) -RuntimeRoot $runtimeRoot
267
+ } else {
268
+ Join-Path $env:USERPROFILE (".codex\channels\telegram-" + $profileAgent)
269
+ }
270
+
271
+ Ensure-Dir -Path $stateDir
272
272
  $envPath = Join-Path $stateDir ".env"
273
273
  $envValues = Read-DotEnvFile -Path $envPath
274
274
 
@@ -281,7 +281,7 @@ $currentAllowedChatIds = [string]$envValues["BLUN_TELEGRAM_ALLOWED_CHAT_ID"]
281
281
  if (-not (Test-AllowedChatIdsFormat -Value $currentAllowedChatIds)) {
282
282
  $currentAllowedChatIds = [string]$envValues["TELEGRAM_ALLOWED_CHAT_ID"]
283
283
  }
284
-
284
+
285
285
  $needsToken = -not (Test-TelegramTokenFormat -Value $currentToken)
286
286
  $needsChatIds = -not (Test-AllowedChatIdsFormat -Value $currentAllowedChatIds)
287
287
  $changed = $false
@@ -322,25 +322,25 @@ if ($EnsureConfigured -and $Json -and $needsToken) {
322
322
  } | ConvertTo-Json -Depth 6
323
323
  exit 2
324
324
  }
325
-
326
- if (-not $Json) {
327
- Write-Host ""
328
- Write-Host "CodexLink Telegram Setup" -ForegroundColor Cyan
329
- Write-Host "Profil: $profileAgent"
330
- Write-Host "Lokaler State-Ordner: $stateDir"
325
+
326
+ if (-not $Json) {
327
+ Write-Host ""
328
+ Write-Host "CodexLink Telegram Setup" -ForegroundColor Cyan
329
+ Write-Host "Profil: $profileAgent"
330
+ Write-Host "Lokaler State-Ordner: $stateDir"
331
331
  Write-Host ""
332
332
  Write-Host "Ich speichere die Telegram-Werte automatisch an die richtige lokale Stelle." -ForegroundColor DarkGray
333
333
  Write-Host "Du musst keine .env-Datei selbst suchen." -ForegroundColor DarkGray
334
334
  Write-Host "Die Chat-ID wird automatisch erkannt. Du musst sie nicht wissen." -ForegroundColor DarkGray
335
335
  Write-Host ""
336
336
  }
337
-
338
- if ($needsToken) {
339
- $currentToken = Prompt-RequiredValue `
340
- -Prompt "Telegram Bot Token" `
341
- -CurrentValue $currentToken `
342
- -Validator { param($v) Test-TelegramTokenFormat -Value $v } `
343
- -ErrorMessage "Bitte einen gueltigen Telegram Bot Token eingeben. Beispiel: 123456789:ABC..."
337
+
338
+ if ($needsToken) {
339
+ $currentToken = Prompt-RequiredValue `
340
+ -Prompt "Telegram Bot Token" `
341
+ -CurrentValue $currentToken `
342
+ -Validator { param($v) Test-TelegramTokenFormat -Value $v } `
343
+ -ErrorMessage "Bitte einen gueltigen Telegram Bot Token eingeben. Beispiel: 123456789:ABC..."
344
344
  $envValues["BLUN_TELEGRAM_BOT_TOKEN"] = $currentToken
345
345
  $changed = $true
346
346
  $tokenWasPrompted = $true
@@ -349,7 +349,8 @@ if ($needsToken) {
349
349
  if ($needsChatIds -and -not $Json) {
350
350
  try {
351
351
  $botInfo = Get-TelegramBotInfo -Token $currentToken
352
- $shouldPair = $tokenWasPrompted -or (-not $EnsureConfigured)
352
+ $pairingDone = [string]$envValues["BLUN_TELEGRAM_PAIRING_DONE"]
353
+ $shouldPair = $tokenWasPrompted -or (-not $EnsureConfigured) -or ($pairingDone -ne "1")
353
354
  if ($shouldPair) {
354
355
  $pairedChat = Wait-TelegramPairingChat -Token $currentToken -BotInfo $botInfo -TimeoutSeconds 90
355
356
  if ($pairedChat -and $pairedChat.chat_id) {
@@ -374,31 +375,31 @@ if ($needsChatIds -and -not $Json) {
374
375
  Write-Host "Ich starte ohne Allowlist; du kannst spaeter erneut `blun-codex telegram-setup` ausfuehren." -ForegroundColor Yellow
375
376
  }
376
377
  }
377
-
378
- $envValues["BLUN_TELEGRAM_AGENT_NAME"] = $profileAgent
379
- $envValues["BLUN_TELEGRAM_STATE_DIR"] = $stateDir
380
- Write-DotEnvFile -Path $envPath -Values $envValues
381
-
382
- $result = [ordered]@{
383
- ok = $true
384
- changed = $changed
385
- profile = $profileAgent
386
- state_dir = $stateDir
387
- env_path = $envPath
388
- missing = @()
389
- }
390
-
391
- if ($Json) {
392
- $result | ConvertTo-Json -Depth 6
393
- exit 0
394
- }
395
-
396
- Write-Host ""
397
- Write-Host "Telegram ist jetzt eingerichtet." -ForegroundColor Green
398
- Write-Host "Gespeichert unter: $envPath"
399
- Write-Host ""
400
- Write-Host "Naechster Schritt:" -ForegroundColor Cyan
401
- Write-Host " blun-codex --profile $profileAgent telegram-plugin"
402
- Write-Host ""
403
- Write-Host "Pruefen kannst du spaeter mit:" -ForegroundColor Cyan
404
- Write-Host " blun-codex --profile $profileAgent telegram-doctor"
378
+
379
+ $envValues["BLUN_TELEGRAM_AGENT_NAME"] = $profileAgent
380
+ $envValues["BLUN_TELEGRAM_STATE_DIR"] = $stateDir
381
+ Write-DotEnvFile -Path $envPath -Values $envValues
382
+
383
+ $result = [ordered]@{
384
+ ok = $true
385
+ changed = $changed
386
+ profile = $profileAgent
387
+ state_dir = $stateDir
388
+ env_path = $envPath
389
+ missing = @()
390
+ }
391
+
392
+ if ($Json) {
393
+ $result | ConvertTo-Json -Depth 6
394
+ exit 0
395
+ }
396
+
397
+ Write-Host ""
398
+ Write-Host "Telegram ist jetzt eingerichtet." -ForegroundColor Green
399
+ Write-Host "Gespeichert unter: $envPath"
400
+ Write-Host ""
401
+ Write-Host "Naechster Schritt:" -ForegroundColor Cyan
402
+ Write-Host " blun-codex --profile $profileAgent telegram-plugin"
403
+ Write-Host ""
404
+ Write-Host "Pruefen kannst du spaeter mit:" -ForegroundColor Cyan
405
+ Write-Host " blun-codex --profile $profileAgent telegram-doctor"