-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathindex.ts
More file actions
803 lines (736 loc) · 27.6 KB
/
index.ts
File metadata and controls
803 lines (736 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/**
* @blockrun/clawrouter
*
* Smart LLM router for OpenClaw — 30+ models, x402 micropayments, 78% cost savings.
* Routes each request to the cheapest model that can handle it.
*
* Usage:
* # Install the plugin
* openclaw plugins install @blockrun/clawrouter
*
* # Fund your wallet with USDC on Base (address printed on install)
*
* # Use smart routing (auto-picks cheapest model)
* openclaw models set blockrun/auto
*
* # Or use any specific BlockRun model
* openclaw models set openai/gpt-5.2
*/
import type {
OpenClawPluginDefinition,
OpenClawPluginApi,
PluginCommandContext,
OpenClawPluginCommandDefinition,
} from "./types.js";
import { blockrunProvider, setActiveProxy } from "./provider.js";
import { startProxy, getProxyPort } from "./proxy.js";
import { resolveOrGenerateWalletKey, WALLET_FILE } from "./auth.js";
import type { RoutingConfig } from "./router/index.js";
import { BalanceMonitor } from "./balance.js";
/**
* Wait for proxy health check to pass (quick check, not RPC).
* Returns true if healthy within timeout, false otherwise.
*/
async function waitForProxyHealth(port: number, timeoutMs = 3000): Promise<boolean> {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
try {
const res = await fetch(`http://127.0.0.1:${port}/health`);
if (res.ok) return true;
} catch {
// Proxy not ready yet
}
await new Promise((r) => setTimeout(r, 100));
}
return false;
}
import { OPENCLAW_MODELS } from "./models.js";
import {
writeFileSync,
existsSync,
readdirSync,
mkdirSync,
copyFileSync,
renameSync,
} from "node:fs";
import { readTextFileSync } from "./fs-read.js";
import { homedir } from "node:os";
import { join } from "node:path";
import { VERSION } from "./version.js";
import { privateKeyToAccount } from "viem/accounts";
import { getStats, formatStatsAscii } from "./stats.js";
/**
* Detect if we're running in shell completion mode.
* When `openclaw completion --shell zsh` runs, it loads plugins but only needs
* the completion script output - any stdout logging pollutes the script and
* causes zsh to interpret colored text like `[plugins]` as glob patterns.
*/
function isCompletionMode(): boolean {
const args = process.argv;
// Check for: openclaw completion --shell <shell>
// argv[0] = node/bun, argv[1] = openclaw, argv[2] = completion
return args.some((arg, i) => arg === "completion" && i >= 1 && i <= 3);
}
/**
* Detect if we're running in gateway mode.
* The proxy should ONLY start when the gateway is running.
* During CLI commands (plugins, models, etc), the proxy keeps the process alive.
*/
function isGatewayMode(): boolean {
const args = process.argv;
// Gateway mode is: openclaw gateway start/restart/stop
return args.includes("gateway");
}
/**
* Inject BlockRun models config into OpenClaw config file.
* This is required because registerProvider() alone doesn't make models available.
*
* CRITICAL: This function must be idempotent and handle ALL edge cases:
* - Config file doesn't exist (create it)
* - Config file exists but is empty/invalid (reinitialize)
* - blockrun provider exists but has undefined fields (fix them)
* - Config exists but uses old port/models (update them)
*
* This function is called on EVERY plugin load to ensure config is always correct.
*/
function injectModelsConfig(logger: { info: (msg: string) => void }): void {
const configDir = join(homedir(), ".openclaw");
const configPath = join(configDir, "openclaw.json");
let config: Record<string, unknown> = {};
let needsWrite = false;
// Create config directory if it doesn't exist
if (!existsSync(configDir)) {
try {
mkdirSync(configDir, { recursive: true });
logger.info("Created OpenClaw config directory");
} catch (err) {
logger.info(
`Failed to create config dir: ${err instanceof Error ? err.message : String(err)}`,
);
return;
}
}
// Load existing config or create new one
// IMPORTANT: On parse failure, we backup and skip writing to avoid clobbering
// other plugins' config (e.g. Telegram channels). This prevents a race condition
// where a partial/corrupt config file causes us to overwrite everything with
// only our models+agents sections.
if (existsSync(configPath)) {
try {
const content = readTextFileSync(configPath).trim();
if (content) {
config = JSON.parse(content);
} else {
logger.info("OpenClaw config is empty, initializing");
needsWrite = true;
}
} catch (err) {
// Config file exists but is corrupt/invalid JSON — likely a partial write
// from another plugin or a race condition during gateway restart.
// Backup the corrupt file and SKIP writing to avoid losing other config.
const backupPath = `${configPath}.backup.${Date.now()}`;
try {
copyFileSync(configPath, backupPath);
logger.info(`Config parse failed, backed up to ${backupPath}`);
} catch {
logger.info("Config parse failed, could not create backup");
}
logger.info(
`Skipping config injection (corrupt file): ${err instanceof Error ? err.message : String(err)}`,
);
return; // Don't write — we'd lose other plugins' config
}
} else {
logger.info("OpenClaw config not found, creating");
needsWrite = true;
}
// Initialize config structure
if (!config.models) {
config.models = {};
needsWrite = true;
}
const models = config.models as Record<string, unknown>;
if (!models.providers) {
models.providers = {};
needsWrite = true;
}
const proxyPort = getProxyPort();
const expectedBaseUrl = `http://127.0.0.1:${proxyPort}/v1`;
const providers = models.providers as Record<string, unknown>;
if (!providers.blockrun) {
// Create new blockrun provider config
providers.blockrun = {
baseUrl: expectedBaseUrl,
api: "openai-completions",
// apiKey is required by pi-coding-agent's ModelRegistry for providers with models.
// We use a placeholder since the proxy handles real x402 auth internally.
apiKey: "x402-proxy-handles-auth",
models: OPENCLAW_MODELS,
};
logger.info("Injected BlockRun provider config");
needsWrite = true;
} else {
// Validate and fix existing blockrun config
const blockrun = providers.blockrun as Record<string, unknown>;
let fixed = false;
// Fix: explicitly check for undefined/missing fields
if (!blockrun.baseUrl || blockrun.baseUrl !== expectedBaseUrl) {
blockrun.baseUrl = expectedBaseUrl;
fixed = true;
}
// Ensure api field is present
if (!blockrun.api) {
blockrun.api = "openai-completions";
fixed = true;
}
// Ensure apiKey is present (required by ModelRegistry for /model picker)
if (!blockrun.apiKey) {
blockrun.apiKey = "x402-proxy-handles-auth";
fixed = true;
}
// Always refresh models list (ensures new models/aliases are available)
// Check both length AND content - new models may be added without changing count
const currentModels = blockrun.models as Array<{ id?: string }>;
const currentModelIds = new Set(
Array.isArray(currentModels) ? currentModels.map((m) => m?.id).filter(Boolean) : [],
);
const expectedModelIds = OPENCLAW_MODELS.map((m) => m.id);
const needsModelUpdate =
!currentModels ||
!Array.isArray(currentModels) ||
currentModels.length !== OPENCLAW_MODELS.length ||
expectedModelIds.some((id) => !currentModelIds.has(id));
if (needsModelUpdate) {
blockrun.models = OPENCLAW_MODELS;
fixed = true;
logger.info(`Updated models list (${OPENCLAW_MODELS.length} models)`);
}
if (fixed) {
logger.info("Fixed incomplete BlockRun provider config");
needsWrite = true;
}
}
// Set blockrun/auto as default model ONLY on first install (not every load!)
// This respects user's model selection and prevents hijacking their choice.
if (!config.agents) {
config.agents = {};
needsWrite = true;
}
const agents = config.agents as Record<string, unknown>;
if (!agents.defaults) {
agents.defaults = {};
needsWrite = true;
}
const defaults = agents.defaults as Record<string, unknown>;
if (!defaults.model) {
defaults.model = {};
needsWrite = true;
}
const model = defaults.model as Record<string, unknown>;
// ONLY set default if no primary model exists (first install)
// Do NOT override user's selection on subsequent loads
if (!model.primary) {
model.primary = "blockrun/auto";
logger.info("Set default model to blockrun/auto (first install)");
needsWrite = true;
}
// Add key model aliases to allowlist for /model picker visibility
// Only add essential aliases, not all 50+ models to avoid config pollution
const KEY_MODEL_ALIASES = [
{ id: "auto", alias: "auto" },
{ id: "eco", alias: "eco" },
{ id: "premium", alias: "premium" },
{ id: "free", alias: "free" },
{ id: "sonnet", alias: "sonnet-4.6" },
{ id: "opus", alias: "opus" },
{ id: "haiku", alias: "haiku" },
{ id: "gpt5", alias: "gpt5" },
{ id: "codex", alias: "codex" },
{ id: "grok-fast", alias: "grok-fast" },
{ id: "grok-code", alias: "grok-code" },
{ id: "deepseek", alias: "deepseek" },
{ id: "reasoner", alias: "reasoner" },
{ id: "kimi", alias: "kimi" },
{ id: "minimax", alias: "minimax" },
{ id: "gemini", alias: "gemini" },
];
// Deprecated aliases to remove from config (cleaned up from picker)
const DEPRECATED_ALIASES = [
"blockrun/nvidia",
"blockrun/gpt",
"blockrun/o3",
"blockrun/grok",
"blockrun/mini",
"blockrun/flash", // removed from picker - use gemini instead
];
if (!defaults.models) {
defaults.models = {};
needsWrite = true;
}
const allowlist = defaults.models as Record<string, unknown>;
// Remove deprecated aliases from config
for (const deprecated of DEPRECATED_ALIASES) {
if (allowlist[deprecated]) {
delete allowlist[deprecated];
logger.info(`Removed deprecated model alias: ${deprecated}`);
needsWrite = true;
}
}
// Add current aliases (and update stale aliases)
for (const m of KEY_MODEL_ALIASES) {
const fullId = `blockrun/${m.id}`;
const existing = allowlist[fullId] as Record<string, unknown> | undefined;
if (!existing) {
allowlist[fullId] = { alias: m.alias };
needsWrite = true;
} else if (existing.alias !== m.alias) {
existing.alias = m.alias;
needsWrite = true;
}
}
// Write config file if any changes were made
// Use atomic write (temp file + rename) to prevent partial writes that could
// corrupt the config and cause other plugins to lose their settings on next load.
if (needsWrite) {
try {
const tmpPath = `${configPath}.tmp.${process.pid}`;
writeFileSync(tmpPath, JSON.stringify(config, null, 2));
renameSync(tmpPath, configPath);
logger.info("Smart routing enabled (blockrun/auto)");
} catch (err) {
logger.info(`Failed to write config: ${err instanceof Error ? err.message : String(err)}`);
}
}
}
/**
* Inject dummy auth profile for BlockRun into agent auth stores.
* OpenClaw's agent system looks for auth credentials even if provider has auth: [].
* We inject a placeholder so the lookup succeeds (proxy handles real auth internally).
*/
function injectAuthProfile(logger: { info: (msg: string) => void }): void {
const agentsDir = join(homedir(), ".openclaw", "agents");
// Create agents directory if it doesn't exist
if (!existsSync(agentsDir)) {
try {
mkdirSync(agentsDir, { recursive: true });
} catch (err) {
logger.info(
`Could not create agents dir: ${err instanceof Error ? err.message : String(err)}`,
);
return;
}
}
try {
// Find all agent directories
let agents = readdirSync(agentsDir, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => d.name);
// Always ensure "main" agent has auth (most common agent)
if (!agents.includes("main")) {
agents = ["main", ...agents];
}
for (const agentId of agents) {
const authDir = join(agentsDir, agentId, "agent");
const authPath = join(authDir, "auth-profiles.json");
// Create agent dir if needed
if (!existsSync(authDir)) {
try {
mkdirSync(authDir, { recursive: true });
} catch {
continue; // Skip if we can't create the dir
}
}
// Load or create auth-profiles.json with correct OpenClaw format
// Format: { version: 1, profiles: { "provider:profileId": { type, provider, key } } }
let store: { version: number; profiles: Record<string, unknown> } = {
version: 1,
profiles: {},
};
if (existsSync(authPath)) {
try {
const existing = JSON.parse(readTextFileSync(authPath));
// Check if valid OpenClaw format (has version and profiles)
if (existing.version && existing.profiles) {
store = existing;
}
// Old format without version/profiles is discarded and recreated
} catch {
// Invalid JSON, use fresh store
}
}
// Check if blockrun auth already exists (OpenClaw format: profiles["provider:profileId"])
const profileKey = "blockrun:default";
if (store.profiles[profileKey]) {
continue; // Already configured
}
// Inject placeholder auth for blockrun (OpenClaw format)
// The proxy handles real x402 auth internally, this just satisfies OpenClaw's lookup
store.profiles[profileKey] = {
type: "api_key",
provider: "blockrun",
key: "x402-proxy-handles-auth",
};
try {
writeFileSync(authPath, JSON.stringify(store, null, 2));
logger.info(`Injected BlockRun auth profile for agent: ${agentId}`);
} catch (err) {
logger.info(
`Could not inject auth for ${agentId}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
} catch (err) {
logger.info(`Auth injection failed: ${err instanceof Error ? err.message : String(err)}`);
}
}
// Store active proxy handle for cleanup on gateway_stop
let activeProxyHandle: Awaited<ReturnType<typeof startProxy>> | null = null;
/**
* Start the x402 proxy in the background.
* Called from register() because OpenClaw's loader only invokes register(),
* treating activate() as an alias (def.register ?? def.activate).
*/
async function startProxyInBackground(api: OpenClawPluginApi): Promise<void> {
// Resolve wallet key: saved file → env var → auto-generate
const { key: walletKey, address, source } = await resolveOrGenerateWalletKey();
// Log wallet source (brief - balance check happens after proxy starts)
if (source === "generated") {
api.logger.info(`Generated new wallet: ${address}`);
} else if (source === "saved") {
api.logger.info(`Using saved wallet: ${address}`);
} else {
api.logger.info(`Using wallet from BLOCKRUN_WALLET_KEY: ${address}`);
}
// Resolve routing config overrides from plugin config
const routingConfig = api.pluginConfig?.routing as Partial<RoutingConfig> | undefined;
const proxy = await startProxy({
walletKey,
routingConfig,
onReady: (port) => {
api.logger.info(`BlockRun x402 proxy listening on port ${port}`);
},
onError: (error) => {
api.logger.error(`BlockRun proxy error: ${error.message}`);
},
onRouted: (decision) => {
const cost = decision.costEstimate.toFixed(4);
const saved = (decision.savings * 100).toFixed(0);
api.logger.info(
`[${decision.tier}] ${decision.model} $${cost} (saved ${saved}%) | ${decision.reasoning}`,
);
},
onLowBalance: (info) => {
api.logger.warn(`[!] Low balance: ${info.balanceUSD}. Fund wallet: ${info.walletAddress}`);
},
onInsufficientFunds: (info) => {
api.logger.error(
`[!] Insufficient funds. Balance: ${info.balanceUSD}, Needed: ${info.requiredUSD}. Fund wallet: ${info.walletAddress}`,
);
},
});
setActiveProxy(proxy);
activeProxyHandle = proxy;
api.logger.info(`ClawRouter ready — smart routing enabled`);
api.logger.info(`Pricing: Simple ~$0.001 | Code ~$0.01 | Complex ~$0.05 | Free: $0`);
// Non-blocking balance check AFTER proxy is ready (won't hang startup)
const startupMonitor = new BalanceMonitor(address);
startupMonitor
.checkBalance()
.then((balance) => {
if (balance.isEmpty) {
api.logger.info(`Wallet: ${address} | Balance: $0.00`);
api.logger.info(`Using FREE model. Fund wallet for premium models.`);
} else if (balance.isLow) {
api.logger.info(`Wallet: ${address} | Balance: ${balance.balanceUSD} (low)`);
} else {
api.logger.info(`Wallet: ${address} | Balance: ${balance.balanceUSD}`);
}
})
.catch(() => {
// Silently continue - balance will be checked per-request anyway
api.logger.info(`Wallet: ${address} | Balance: (checking...)`);
});
}
/**
* /stats command handler for ClawRouter.
* Shows usage statistics and cost savings.
*/
async function createStatsCommand(): Promise<OpenClawPluginCommandDefinition> {
return {
name: "stats",
description: "Show ClawRouter usage statistics and cost savings",
acceptsArgs: true,
requireAuth: false,
handler: async (ctx: PluginCommandContext) => {
const arg = ctx.args?.trim().toLowerCase() || "7";
const days = parseInt(arg, 10) || 7;
try {
const stats = await getStats(Math.min(days, 30)); // Cap at 30 days
const ascii = formatStatsAscii(stats);
return {
text: ["```", ascii, "```"].join("\n"),
};
} catch (err) {
return {
text: `Failed to load stats: ${err instanceof Error ? err.message : String(err)}`,
isError: true,
};
}
},
};
}
/**
* /wallet command handler for ClawRouter.
* - /wallet or /wallet status: Show wallet address, balance, and key file location
* - /wallet export: Show private key for backup (with security warning)
*/
async function createWalletCommand(): Promise<OpenClawPluginCommandDefinition> {
return {
name: "wallet",
description: "Show BlockRun wallet info or export private key for backup",
acceptsArgs: true,
requireAuth: true,
handler: async (ctx: PluginCommandContext) => {
const subcommand = ctx.args?.trim().toLowerCase() || "status";
// Read wallet key if it exists
let walletKey: string | undefined;
let address: string | undefined;
try {
if (existsSync(WALLET_FILE)) {
walletKey = readTextFileSync(WALLET_FILE).trim();
if (walletKey.startsWith("0x") && walletKey.length === 66) {
const account = privateKeyToAccount(walletKey as `0x${string}`);
address = account.address;
}
}
} catch {
// Wallet file doesn't exist or is invalid
}
if (!walletKey || !address) {
return {
text: `No ClawRouter wallet found.\n\nRun \`openclaw plugins install @blockrun/clawrouter\` to generate a wallet.`,
isError: true,
};
}
if (subcommand === "export") {
// Export private key for backup
return {
text: [
"🔐 **ClawRouter Wallet Export**",
"",
"⚠️ **SECURITY WARNING**: Your private key controls your wallet funds.",
"Never share this key. Anyone with this key can spend your USDC.",
"",
`**Address:** \`${address}\``,
"",
`**Private Key:**`,
`\`${walletKey}\``,
"",
"**To restore on a new machine:**",
"1. Set the environment variable before running OpenClaw:",
` \`export BLOCKRUN_WALLET_KEY=${walletKey}\``,
"2. Or save to file:",
` \`mkdir -p ~/.openclaw/blockrun && echo "${walletKey}" > ~/.openclaw/blockrun/wallet.key && chmod 600 ~/.openclaw/blockrun/wallet.key\``,
].join("\n"),
};
}
// Default: show wallet status
let balanceText = "Balance: (checking...)";
try {
const monitor = new BalanceMonitor(address);
const balance = await monitor.checkBalance();
balanceText = `Balance: ${balance.balanceUSD}`;
} catch {
balanceText = "Balance: (could not check)";
}
return {
text: [
"🦞 **ClawRouter Wallet**",
"",
`**Address:** \`${address}\``,
`**${balanceText}**`,
`**Key File:** \`${WALLET_FILE}\``,
"",
"**Commands:**",
"• `/wallet` - Show this status",
"• `/wallet export` - Export private key for backup",
"",
`**Fund with USDC on Base:** https://basescan.org/address/${address}`,
].join("\n"),
};
},
};
}
const plugin: OpenClawPluginDefinition = {
id: "clawrouter",
name: "ClawRouter",
description: "Smart LLM router — 30+ models, x402 micropayments, 78% cost savings",
version: VERSION,
async register(api: OpenClawPluginApi) {
// Check if ClawRouter is disabled via environment variable
// Usage: CLAWROUTER_DISABLED=true openclaw gateway start
const isDisabled =
process["env"].CLAWROUTER_DISABLED === "true" || process["env"].CLAWROUTER_DISABLED === "1";
if (isDisabled) {
api.logger.info("ClawRouter disabled (CLAWROUTER_DISABLED=true). Using default routing.");
return;
}
// Skip heavy initialization in completion mode — only completion script is needed
// Logging to stdout during completion pollutes the script and causes zsh errors
if (isCompletionMode()) {
api.registerProvider(blockrunProvider);
return;
}
// Register BlockRun as a provider (sync — available immediately)
api.registerProvider(blockrunProvider);
// Inject models config into OpenClaw config file
// This persists the config so models are recognized on restart
injectModelsConfig(api.logger);
// Inject dummy auth profiles into agent auth stores
// OpenClaw's agent system looks for auth even if provider has auth: []
injectAuthProfile(api.logger);
// Also set runtime config for immediate availability
const runtimePort = getProxyPort();
if (!api.config.models) {
api.config.models = { providers: {} };
}
if (!api.config.models.providers) {
api.config.models.providers = {};
}
api.config.models.providers.blockrun = {
baseUrl: `http://127.0.0.1:${runtimePort}/v1`,
api: "openai-completions",
// apiKey is required by pi-coding-agent's ModelRegistry for providers with models.
apiKey: "x402-proxy-handles-auth",
models: OPENCLAW_MODELS,
};
// Set blockrun/auto as default ONLY if no model is set (first install)
// Do NOT override user's model selection on subsequent loads
if (!api.config.agents) api.config.agents = {};
const agents = api.config.agents as Record<string, unknown>;
if (!agents.defaults) agents.defaults = {};
const defaults = agents.defaults as Record<string, unknown>;
if (!defaults.model) defaults.model = {};
const model = defaults.model as Record<string, unknown>;
if (!model.primary) {
model.primary = "blockrun/auto";
}
api.logger.info("BlockRun provider registered (30+ models via x402)");
// Register /wallet command for wallet management
createWalletCommand()
.then((walletCommand) => {
api.registerCommand(walletCommand);
})
.catch((err) => {
api.logger.warn(
`Failed to register /wallet command: ${err instanceof Error ? err.message : String(err)}`,
);
});
// Register /stats command for usage statistics
createStatsCommand()
.then((statsCommand) => {
api.registerCommand(statsCommand);
})
.catch((err) => {
api.logger.warn(
`Failed to register /stats command: ${err instanceof Error ? err.message : String(err)}`,
);
});
// Register a service with stop() for cleanup on gateway shutdown
// This prevents EADDRINUSE when the gateway restarts
api.registerService({
id: "clawrouter-proxy",
start: () => {
// No-op: proxy is started below in non-blocking mode
},
stop: async () => {
// Close proxy on gateway shutdown to release port 8402
if (activeProxyHandle) {
try {
await activeProxyHandle.close();
api.logger.info("BlockRun proxy closed");
} catch (err) {
api.logger.warn(
`Failed to close proxy: ${err instanceof Error ? err.message : String(err)}`,
);
}
activeProxyHandle = null;
}
},
});
// Skip proxy startup unless we're in gateway mode
// The proxy keeps the Node.js event loop alive, preventing CLI commands from exiting
// The proxy will start automatically when the gateway runs
if (!isGatewayMode()) {
api.logger.info("Not in gateway mode — proxy will start when gateway runs");
return;
}
// Start x402 proxy in background WITHOUT blocking register()
// CRITICAL: Do NOT await here - this was blocking model selection UI for 3+ seconds
// causing Chandler's "infinite loop" issue where model selection never finishes
startProxyInBackground(api)
.then(async () => {
// Proxy started successfully - verify health
const port = getProxyPort();
const healthy = await waitForProxyHealth(port, 5000);
if (!healthy) {
api.logger.warn(`Proxy health check timed out, commands may not work immediately`);
}
})
.catch((err) => {
api.logger.error(
`Failed to start BlockRun proxy: ${err instanceof Error ? err.message : String(err)}`,
);
});
},
};
export default plugin;
// Re-export for programmatic use
export { startProxy, getProxyPort } from "./proxy.js";
export type { ProxyOptions, ProxyHandle, LowBalanceInfo, InsufficientFundsInfo } from "./proxy.js";
export { blockrunProvider } from "./provider.js";
export {
OPENCLAW_MODELS,
BLOCKRUN_MODELS,
buildProviderModels,
MODEL_ALIASES,
resolveModelAlias,
isAgenticModel,
getAgenticModels,
getModelContextWindow,
} from "./models.js";
export {
route,
DEFAULT_ROUTING_CONFIG,
getFallbackChain,
getFallbackChainFiltered,
calculateModelCost,
} from "./router/index.js";
export type { RoutingDecision, RoutingConfig, Tier } from "./router/index.js";
export { logUsage } from "./logger.js";
export type { UsageEntry } from "./logger.js";
export { RequestDeduplicator } from "./dedup.js";
export type { CachedResponse } from "./dedup.js";
export { PaymentCache } from "./payment-cache.js";
export type { CachedPaymentParams } from "./payment-cache.js";
export { createPaymentFetch } from "./x402.js";
export type { PreAuthParams, PaymentFetchResult } from "./x402.js";
export { BalanceMonitor, BALANCE_THRESHOLDS } from "./balance.js";
export type { BalanceInfo, SufficiencyResult } from "./balance.js";
export {
InsufficientFundsError,
EmptyWalletError,
RpcError,
isInsufficientFundsError,
isEmptyWalletError,
isBalanceError,
isRpcError,
} from "./errors.js";
export { fetchWithRetry, isRetryable, DEFAULT_RETRY_CONFIG } from "./retry.js";
export type { RetryConfig } from "./retry.js";
export { getStats, formatStatsAscii } from "./stats.js";
export type { DailyStats, AggregatedStats } from "./stats.js";
export { SessionStore, getSessionId, DEFAULT_SESSION_CONFIG } from "./session.js";
export type { SessionEntry, SessionConfig } from "./session.js";
export { ResponseCache } from "./response-cache.js";
export type { CachedLLMResponse, ResponseCacheConfig } from "./response-cache.js";