@blockrun/clawrouter 0.7.0 → 0.8.0

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/dist/index.js CHANGED
@@ -3502,80 +3502,137 @@ function isCompletionMode() {
3502
3502
  return args.some((arg, i) => arg === "completion" && i >= 1 && i <= 3);
3503
3503
  }
3504
3504
  function injectModelsConfig(logger) {
3505
- const configPath = join5(homedir4(), ".openclaw", "openclaw.json");
3506
- if (!existsSync(configPath)) {
3507
- logger.info("OpenClaw config not found, skipping models injection");
3508
- return;
3505
+ const configDir = join5(homedir4(), ".openclaw");
3506
+ const configPath = join5(configDir, "openclaw.json");
3507
+ let config = {};
3508
+ let needsWrite = false;
3509
+ if (!existsSync(configDir)) {
3510
+ try {
3511
+ mkdirSync(configDir, { recursive: true });
3512
+ logger.info("Created OpenClaw config directory");
3513
+ } catch (err) {
3514
+ logger.info(`Failed to create config dir: ${err instanceof Error ? err.message : String(err)}`);
3515
+ return;
3516
+ }
3509
3517
  }
3510
- try {
3511
- const config = JSON.parse(readFileSync(configPath, "utf-8"));
3512
- let needsWrite = false;
3513
- if (!config.models) config.models = {};
3514
- if (!config.models.providers) config.models.providers = {};
3515
- const proxyPort = getProxyPort();
3516
- const expectedBaseUrl = `http://127.0.0.1:${proxyPort}/v1`;
3517
- if (!config.models.providers.blockrun) {
3518
- config.models.providers.blockrun = {
3519
- baseUrl: expectedBaseUrl,
3520
- api: "openai-completions",
3521
- // apiKey is required by pi-coding-agent's ModelRegistry for providers with models.
3522
- // We use a placeholder since the proxy handles real x402 auth internally.
3523
- apiKey: "x402-proxy-handles-auth",
3524
- models: OPENCLAW_MODELS
3525
- };
3526
- needsWrite = true;
3527
- } else {
3528
- if (config.models.providers.blockrun.baseUrl !== expectedBaseUrl) {
3529
- config.models.providers.blockrun.baseUrl = expectedBaseUrl;
3530
- needsWrite = true;
3531
- }
3532
- if (!config.models.providers.blockrun.apiKey) {
3533
- config.models.providers.blockrun.apiKey = "x402-proxy-handles-auth";
3534
- needsWrite = true;
3535
- }
3536
- const currentModels = config.models.providers.blockrun.models;
3537
- if (!currentModels || currentModels.length !== OPENCLAW_MODELS.length) {
3538
- config.models.providers.blockrun.models = OPENCLAW_MODELS;
3518
+ if (existsSync(configPath)) {
3519
+ try {
3520
+ const content = readFileSync(configPath, "utf-8").trim();
3521
+ if (content) {
3522
+ config = JSON.parse(content);
3523
+ } else {
3524
+ logger.info("OpenClaw config is empty, initializing");
3539
3525
  needsWrite = true;
3540
3526
  }
3527
+ } catch (err) {
3528
+ logger.info(`Failed to parse config (will recreate): ${err instanceof Error ? err.message : String(err)}`);
3529
+ config = {};
3530
+ needsWrite = true;
3541
3531
  }
3542
- if (!config.agents) config.agents = {};
3543
- if (!config.agents.defaults) config.agents.defaults = {};
3544
- if (!config.agents.defaults.model) config.agents.defaults.model = {};
3545
- if (config.agents.defaults.model.primary !== "blockrun/auto") {
3546
- config.agents.defaults.model.primary = "blockrun/auto";
3532
+ } else {
3533
+ logger.info("OpenClaw config not found, creating");
3534
+ needsWrite = true;
3535
+ }
3536
+ if (!config.models) {
3537
+ config.models = {};
3538
+ needsWrite = true;
3539
+ }
3540
+ const models = config.models;
3541
+ if (!models.providers) {
3542
+ models.providers = {};
3543
+ needsWrite = true;
3544
+ }
3545
+ const proxyPort = getProxyPort();
3546
+ const expectedBaseUrl = `http://127.0.0.1:${proxyPort}/v1`;
3547
+ const providers = models.providers;
3548
+ if (!providers.blockrun) {
3549
+ providers.blockrun = {
3550
+ baseUrl: expectedBaseUrl,
3551
+ api: "openai-completions",
3552
+ // apiKey is required by pi-coding-agent's ModelRegistry for providers with models.
3553
+ // We use a placeholder since the proxy handles real x402 auth internally.
3554
+ apiKey: "x402-proxy-handles-auth",
3555
+ models: OPENCLAW_MODELS
3556
+ };
3557
+ logger.info("Injected BlockRun provider config");
3558
+ needsWrite = true;
3559
+ } else {
3560
+ const blockrun = providers.blockrun;
3561
+ let fixed = false;
3562
+ if (!blockrun.baseUrl || blockrun.baseUrl !== expectedBaseUrl) {
3563
+ blockrun.baseUrl = expectedBaseUrl;
3564
+ fixed = true;
3565
+ }
3566
+ if (!blockrun.api) {
3567
+ blockrun.api = "openai-completions";
3568
+ fixed = true;
3569
+ }
3570
+ if (!blockrun.apiKey) {
3571
+ blockrun.apiKey = "x402-proxy-handles-auth";
3572
+ fixed = true;
3573
+ }
3574
+ const currentModels = blockrun.models;
3575
+ if (!currentModels || !Array.isArray(currentModels) || currentModels.length !== OPENCLAW_MODELS.length) {
3576
+ blockrun.models = OPENCLAW_MODELS;
3577
+ fixed = true;
3578
+ }
3579
+ if (fixed) {
3580
+ logger.info("Fixed incomplete BlockRun provider config");
3547
3581
  needsWrite = true;
3548
3582
  }
3549
- const KEY_MODEL_ALIASES = [
3550
- { id: "auto", alias: "auto" },
3551
- { id: "free", alias: "free" },
3552
- { id: "sonnet", alias: "sonnet" },
3553
- { id: "opus", alias: "opus" },
3554
- { id: "haiku", alias: "haiku" },
3555
- { id: "grok", alias: "grok" },
3556
- { id: "deepseek", alias: "deepseek" },
3557
- { id: "kimi", alias: "kimi" },
3558
- { id: "gemini", alias: "gemini" },
3559
- { id: "flash", alias: "flash" },
3560
- { id: "gpt", alias: "gpt" },
3561
- { id: "reasoner", alias: "reasoner" }
3562
- ];
3563
- if (!config.agents) config.agents = {};
3564
- if (!config.agents.defaults) config.agents.defaults = {};
3565
- if (!config.agents.defaults.models) config.agents.defaults.models = {};
3566
- const allowlist = config.agents.defaults.models;
3567
- for (const m of KEY_MODEL_ALIASES) {
3568
- const fullId = `blockrun/${m.id}`;
3569
- if (!allowlist[fullId]) {
3570
- allowlist[fullId] = { alias: m.alias };
3571
- needsWrite = true;
3572
- }
3583
+ }
3584
+ if (!config.agents) {
3585
+ config.agents = {};
3586
+ needsWrite = true;
3587
+ }
3588
+ const agents = config.agents;
3589
+ if (!agents.defaults) {
3590
+ agents.defaults = {};
3591
+ needsWrite = true;
3592
+ }
3593
+ const defaults = agents.defaults;
3594
+ if (!defaults.model) {
3595
+ defaults.model = {};
3596
+ needsWrite = true;
3597
+ }
3598
+ const model = defaults.model;
3599
+ if (model.primary !== "blockrun/auto") {
3600
+ model.primary = "blockrun/auto";
3601
+ needsWrite = true;
3602
+ }
3603
+ const KEY_MODEL_ALIASES = [
3604
+ { id: "auto", alias: "auto" },
3605
+ { id: "free", alias: "free" },
3606
+ { id: "sonnet", alias: "sonnet" },
3607
+ { id: "opus", alias: "opus" },
3608
+ { id: "haiku", alias: "haiku" },
3609
+ { id: "grok", alias: "grok" },
3610
+ { id: "deepseek", alias: "deepseek" },
3611
+ { id: "kimi", alias: "kimi" },
3612
+ { id: "gemini", alias: "gemini" },
3613
+ { id: "flash", alias: "flash" },
3614
+ { id: "gpt", alias: "gpt" },
3615
+ { id: "reasoner", alias: "reasoner" }
3616
+ ];
3617
+ if (!defaults.models) {
3618
+ defaults.models = {};
3619
+ needsWrite = true;
3620
+ }
3621
+ const allowlist = defaults.models;
3622
+ for (const m of KEY_MODEL_ALIASES) {
3623
+ const fullId = `blockrun/${m.id}`;
3624
+ if (!allowlist[fullId]) {
3625
+ allowlist[fullId] = { alias: m.alias };
3626
+ needsWrite = true;
3573
3627
  }
3574
- if (needsWrite) {
3628
+ }
3629
+ if (needsWrite) {
3630
+ try {
3575
3631
  writeFileSync(configPath, JSON.stringify(config, null, 2));
3576
3632
  logger.info("Smart routing enabled (blockrun/auto)");
3633
+ } catch (err) {
3634
+ logger.info(`Failed to write config: ${err instanceof Error ? err.message : String(err)}`);
3577
3635
  }
3578
- } catch {
3579
3636
  }
3580
3637
  }
3581
3638
  function injectAuthProfile(logger) {