add-token-config.ps1 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # add-token-config.ps1
  2. # 批量给所有 api-config.json 添加 tokenConfig + errorHandling
  3. # 并将 endpoint-based skill 的 Authorization 改为 {{vocToken}} 动态引用
  4. param(
  5. [string]$SkillsRoot = (Split-Path $PSScriptRoot -Parent),
  6. [switch]$DryRun
  7. )
  8. $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
  9. $hardcodedToken = 'r:858b3ee92314d5447d1fc3cdc10462d7'
  10. # ============================================================
  11. # tokenConfig + errorHandling 块(插入到最后一个 } 之前)
  12. # ============================================================
  13. $injection = @'
  14. ,
  15. "tokenConfig": {
  16. "type": "bearer",
  17. "configFile": "~/.openclaw/voc-credentials.json",
  18. "tokenField": "vocToken",
  19. "currentToken": "r:858b3ee92314d5447d1fc3cdc10462d7",
  20. "resolutionOrder": ["configFile", "currentToken"],
  21. "onMissing": {
  22. "action": "showPaymentPrompt",
  23. "paymentUrl": "",
  24. "title": "需要配置 VOC-AI Token",
  25. "message": "此 Skill 需要有效的 VOC-AI Token 才能调用数据接口。",
  26. "setupInstructions": "充值成功后,请将获得的 Token(格式: r:xxxxxxxx)告知 AI,AI 将自动创建或更新 ~/.openclaw/voc-credentials.json,写入 {\"vocToken\": \"您的Token值\"} 并保存,后续所有 VOC Skills 将自动使用此 Token。"
  27. },
  28. "onBalanceInsufficient": {
  29. "action": "showPaymentPrompt",
  30. "paymentUrl": "",
  31. "title": "VOC-AI Token 余额不足",
  32. "message": "当前 Token 余额已不足以完成本次请求,请充值后告知 AI 更新 ~/.openclaw/voc-credentials.json 中的 vocToken 字段。"
  33. }
  34. },
  35. "errorHandling": {
  36. "balanceInsufficient": {
  37. "conditions": [
  38. { "responseField": "code", "operator": "in", "value": [-2, -3, -10, 402, 429] },
  39. { "responseField": "msg", "operator": "contains", "value": ["余额不足", "insufficient", "balance", "quota"] },
  40. { "responseField": "message", "operator": "contains", "value": ["余额不足", "insufficient", "balance"] }
  41. ],
  42. "matchMode": "any",
  43. "trigger": "tokenConfig.onBalanceInsufficient"
  44. },
  45. "unauthorized": {
  46. "conditions": [
  47. { "responseField": "code", "operator": "in", "value": [401, 403] },
  48. { "responseField": "msg", "operator": "contains", "value": ["unauthorized", "token", "invalid", "auth"] }
  49. ],
  50. "matchMode": "any",
  51. "trigger": "tokenConfig.onMissing"
  52. }
  53. }
  54. '@
  55. $categories = @("voc","social-media","competitor-analysis","review-analysis","synthesis","social-voc","douyin")
  56. $updated = 0
  57. $skipped = 0
  58. foreach ($cat in $categories) {
  59. $catPath = Join-Path $SkillsRoot $cat
  60. if (-not (Test-Path $catPath)) { continue }
  61. Get-ChildItem -Recurse -Filter "api-config.json" -Path $catPath | ForEach-Object {
  62. $filePath = $_.FullName
  63. $content = [System.IO.File]::ReadAllText($filePath, [System.Text.Encoding]::UTF8)
  64. # 跳过已处理的文件
  65. if ($content -match '"tokenConfig"') {
  66. Write-Host " [SKIP] already patched: $($filePath.Replace($SkillsRoot+'\',''))" -ForegroundColor DarkGray
  67. $skipped++
  68. return
  69. }
  70. # --- Step A: Authorization 改为 {{vocToken}}(仅 endpoint-based skill)---
  71. $hasEndpoint = $content -match '"endpoint"'
  72. if ($hasEndpoint) {
  73. $content = $content.Replace(
  74. "Bearer $hardcodedToken",
  75. "Bearer {{vocToken}}"
  76. )
  77. }
  78. # --- Step B: 插入 tokenConfig + errorHandling(最后一个 } 之前)---
  79. $lastBrace = $content.LastIndexOf('}')
  80. if ($lastBrace -lt 0) {
  81. Write-Host " [ERROR] malformed JSON: $filePath" -ForegroundColor Red
  82. return
  83. }
  84. $newContent = $content.Substring(0, $lastBrace) + $injection + "`n}"
  85. if ($DryRun) {
  86. Write-Host " [DRY] $($filePath.Replace($SkillsRoot+'\',''))" -ForegroundColor Yellow
  87. } else {
  88. [System.IO.File]::WriteAllText($filePath, $newContent, $utf8NoBom)
  89. Write-Host " [OK] $($filePath.Replace($SkillsRoot+'\',''))" -ForegroundColor Green
  90. }
  91. $updated++
  92. }
  93. }
  94. Write-Host ""
  95. Write-Host "Done: $updated updated, $skipped skipped"
  96. if ($DryRun) { Write-Host "Remove -DryRun to apply." -ForegroundColor Yellow }