@blockrun/clawrouter 0.12.111 → 0.12.112

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockrun/clawrouter",
3
- "version": "0.12.111",
3
+ "version": "0.12.112",
4
4
  "description": "Smart LLM router — save 85% on inference costs. 55+ models (11 free), one wallet, x402 micropayments.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -62,6 +62,20 @@ run_dependency_install() {
62
62
 
63
63
  trap restore_previous_install EXIT
64
64
 
65
+ # Pre-flight: validate openclaw.json is parseable before touching anything
66
+ validate_config() {
67
+ local config_path="$HOME/.openclaw/openclaw.json"
68
+ if [ ! -f "$config_path" ]; then return 0; fi
69
+ if ! node -e "JSON.parse(require('fs').readFileSync('$config_path','utf8'))" 2>/dev/null; then
70
+ echo ""
71
+ echo "✗ openclaw.json is corrupt (invalid JSON)."
72
+ echo " Fix it first: openclaw doctor --fix"
73
+ echo " Then re-run this script."
74
+ echo ""
75
+ exit 1
76
+ fi
77
+ }
78
+
65
79
  kill_port_processes() {
66
80
  local port="$1"
67
81
  local pids=""
@@ -87,6 +101,9 @@ kill_port_processes() {
87
101
  echo "🦞 ClawRouter Reinstall"
88
102
  echo ""
89
103
 
104
+ # Pre-flight: fail fast if config is corrupt
105
+ validate_config
106
+
90
107
  # 0. Back up wallet key BEFORE removing anything
91
108
  echo "→ Backing up wallet..."
92
109
  if [ -f "$WALLET_FILE" ]; then
@@ -312,6 +329,26 @@ if [ -d "$CREDS_DIR" ] && [ "$(ls -A "$CREDS_DIR" 2>/dev/null)" ]; then
312
329
  echo " ✓ Backed up OpenClaw credentials"
313
330
  fi
314
331
 
332
+ # Extract channel config (Telegram tokens, etc.) from openclaw.json before install
333
+ # openclaw plugins install can overwrite config and wipe channel settings
334
+ CHANNEL_CONFIG_BACKUP=""
335
+ if [ -f "$CONFIG_PATH" ]; then
336
+ CHANNEL_CONFIG_BACKUP="$(mktemp)"
337
+ node -e "
338
+ const fs = require('fs');
339
+ try {
340
+ const config = JSON.parse(fs.readFileSync('$CONFIG_PATH', 'utf8'));
341
+ // Save channels block and gateway block (gateway.mode etc.)
342
+ const preserved = {};
343
+ if (config.channels) preserved.channels = config.channels;
344
+ if (config.gateway) preserved.gateway = config.gateway;
345
+ fs.writeFileSync('$CHANNEL_CONFIG_BACKUP', JSON.stringify(preserved, null, 2));
346
+ const channelCount = Object.keys(config.channels || {}).length;
347
+ if (channelCount > 0) console.log(' ✓ Preserved config for channels: ' + Object.keys(config.channels).join(', '));
348
+ } catch (e) { fs.writeFileSync('$CHANNEL_CONFIG_BACKUP', '{}'); }
349
+ "
350
+ fi
351
+
315
352
  echo "→ Installing ClawRouter..."
316
353
  openclaw plugins install @blockrun/clawrouter
317
354
 
@@ -323,6 +360,59 @@ if [ -n "$CREDS_BACKUP" ] && [ -d "$CREDS_BACKUP" ]; then
323
360
  rm -rf "$(dirname "$CREDS_BACKUP")"
324
361
  fi
325
362
 
363
+ # Restore channel config (Telegram tokens etc.) that may have been wiped by plugin install
364
+ if [ -n "$CHANNEL_CONFIG_BACKUP" ] && [ -f "$CHANNEL_CONFIG_BACKUP" ] && [ -f "$CONFIG_PATH" ]; then
365
+ node -e "
366
+ const fs = require('fs');
367
+ function atomicWrite(filePath, data) {
368
+ const tmpPath = filePath + '.tmp.' + process.pid;
369
+ fs.writeFileSync(tmpPath, data);
370
+ fs.renameSync(tmpPath, filePath);
371
+ }
372
+ try {
373
+ const config = JSON.parse(fs.readFileSync('$CONFIG_PATH', 'utf8'));
374
+ const preserved = JSON.parse(fs.readFileSync('$CHANNEL_CONFIG_BACKUP', 'utf8'));
375
+ let changed = false;
376
+
377
+ // Restore channels block if it was wiped or is now empty
378
+ if (preserved.channels && Object.keys(preserved.channels).length > 0) {
379
+ if (!config.channels || Object.keys(config.channels).length === 0) {
380
+ config.channels = preserved.channels;
381
+ changed = true;
382
+ console.log(' ✓ Restored channel config (Telegram/WhatsApp/etc.)');
383
+ } else {
384
+ // Merge: restore any channels that are now missing
385
+ let merged = 0;
386
+ for (const [ch, val] of Object.entries(preserved.channels)) {
387
+ if (!config.channels[ch]) {
388
+ config.channels[ch] = val;
389
+ merged++;
390
+ }
391
+ }
392
+ if (merged > 0) {
393
+ changed = true;
394
+ console.log(' ✓ Merged ' + merged + ' missing channel(s) back into config');
395
+ } else {
396
+ console.log(' Channel config intact');
397
+ }
398
+ }
399
+ }
400
+
401
+ // Restore gateway.mode if missing
402
+ if (preserved.gateway?.mode && (!config.gateway || !config.gateway.mode)) {
403
+ if (!config.gateway) config.gateway = {};
404
+ config.gateway.mode = preserved.gateway.mode;
405
+ changed = true;
406
+ }
407
+
408
+ if (changed) atomicWrite('$CONFIG_PATH', JSON.stringify(config, null, 2));
409
+ } catch (e) {
410
+ console.log(' Warning: could not restore channel config:', e.message);
411
+ }
412
+ "
413
+ rm -f "$CHANNEL_CONFIG_BACKUP"
414
+ fi
415
+
326
416
  # 6.1. Verify installation and force-update if openclaw installed a stale cached version
327
417
  echo "→ Verifying installation..."
328
418
  DIST_PATH="$PLUGIN_DIR/dist/index.js"
@@ -552,6 +642,21 @@ if (fs.existsSync(configPath)) {
552
642
  }
553
643
  "
554
644
 
645
+ # Clean up stale plugin backups — these cause "duplicate plugin" warnings on restart
646
+ echo "→ Cleaning up stale plugin backups..."
647
+ CLEANED=0
648
+ for backup_dir in "$HOME/.openclaw/extensions/clawrouter.backup."*; do
649
+ if [ -d "$backup_dir" ]; then
650
+ rm -rf "$backup_dir"
651
+ CLEANED=$((CLEANED + 1))
652
+ fi
653
+ done
654
+ if [ "$CLEANED" -gt 0 ]; then
655
+ echo " ✓ Removed $CLEANED stale backup(s)"
656
+ else
657
+ echo " ✓ No stale backups found"
658
+ fi
659
+
555
660
  # Final: verify wallet survived reinstall
556
661
  echo "→ Verifying wallet integrity..."
557
662
  if [ -f "$WALLET_FILE" ]; then