Skip to content

feat: Local Claude CLI integration to skip x402 for Anthropic models - #15

Closed
gigi-trifle wants to merge 182 commits into
BlockRunAI:mainfrom
gigi-trifle:feature/claude-cli-integration
Closed

gigi-trifle wants to merge 182 commits into
BlockRunAI:mainfrom
gigi-trifle:feature/claude-cli-integration

Conversation

@gigi-trifle

Copy link
Copy Markdown
Contributor

Implement the feature from issue #14. Adds config option to route Anthropic models to local Claude CLI instance instead of x402 payments. Includes selector logic, proxy handler, and config injection. POC tested with basic requests. Closes #14.

1bcMax added 30 commits February 3, 2026 12:31
Rewrite README as the full architecture plan for the x402 paid skill
marketplace. BlockRun becomes a payment router between OpenClaw skill
creators (who earn USDC) and end users (who pay per execution via x402
or Stripe). Rename package from openclaw-provider to openclaw-x402.
Research into OpenClaw GitHub issues showed zero demand for paid
skill marketplace but overwhelming demand for cost optimization.
Rewrote README and package around one API key, 30+ models, smart
routing, and spend controls.
x402 is already built and working. Adding Stripe would mean
building accounts, balance ledger, API key management — a second
product. Operators already manage API keys; a wallet key is simpler.
Plugin generates a wallet on first run, user just funds it.
Or bring your own wallet via BLOCKRUN_WALLET_KEY env var.
- Pricing: explain upfront max-token estimation model
- Smart routing: server-side only, remove router.ts from plugin
- Spend controls: two layers (wallet balance + API-enforced budgets)
- Key security: Foundry-style encrypted keystore for auto-generated wallets
- Add Wallet Status and Error Handling sections
- Add /api/v1/balance endpoint to API spec
- Fix roadmap: "one wallet" not "one API key"
- Design doc: docs/plans/2026-02-03-smart-routing-design.md
  4-tier classification (SIMPLE/MEDIUM/COMPLEX/REASONING),
  hybrid rules-first + LLM fallback, declarative routing config,
  per-tier fallback chains, RoutingDecision metadata

- README: Updated smart routing section with hybrid approach,
  architecture with src/router/ directory, reordered roadmap,
  added estimated savings table and customization examples
Smart routing (src/router/):
- Hybrid rules-first classifier: scores token count, code presence,
  reasoning markers, technical terms, multi-step patterns
- 4 tiers: SIMPLE → gemini-flash, MEDIUM → deepseek-chat,
  COMPLEX → claude-sonnet-4, REASONING → o3
- LLM fallback: ambiguous cases classified via gemini-flash (~$0.00003)
- Per-tier fallback chains for provider failures
- RoutingDecision metadata on every routed request
- Declarative config (DEFAULT_ROUTING_CONFIG), operator-overridable

Usage logging (src/logger.ts):
- JSON lines to ~/.openclaw/blockrun/logs/usage-YYYY-MM-DD.jsonl
- Logs model, tier, method, confidence, cost, savings, latency
- Fire-and-forget, never breaks request flow

Integration:
- proxy.ts: detects model="blockrun/auto", runs router, replaces model
- index.ts: passes routing config, logs routing decisions via onRouted
- models.ts: added blockrun/auto meta-model, exported BLOCKRUN_MODELS
- resolveOrGenerateWalletKey(): saved file → env var → auto-generate
- Save wallet to ~/.openclaw/blockrun/wallet.key (mode 0o600)
- Add User-Agent: claw-router/0.1.0 to all proxy requests
- Log wallet address and source on startup
- Add openclaw.extensions to package.json
- Add openclaw.plugin.json with config schema
- Bump version to 0.1.1
- Replace @x402/fetch library with custom x402.ts implementation
- Fix content-length header issue when routing modifies request body
- Add test results section to README showcasing 94% savings
- GitHub repo: BlockRunAI/ClawRouter
- npm package: @blockrun/clawrouter
- Update all README references
- Add skills/clawrouter/SKILL.md for ClawHub marketplace listing
- Update openclaw.plugin.json id from claw-router to clawrouter
- Remove pricing formula from README
…eline

- COMPLEX primary: claude-sonnet-4 → claude-opus-4 (best quality)
- Savings baseline: GPT-4o → Claude Opus (the premium default)
- Headline: 63% → 78% savings (weighted avg $16.17/M vs Opus $75/M)
- SIMPLE: 94% → 99%, REASONING: 20% → 89% (vs Opus instead of GPT-4o)
- All 19 tests pass with new baseline
Replace integer scoring with weighted float scoring across 14 dimensions.
Add 6 new dimensions: imperative verbs, constraint count, output format,
reference complexity, negation complexity, domain specificity.
Confidence calibrated via sigmoid of distance from tier boundary.
Configurable thresholds replace fixed ambiguous zone [1,2].
- Rename Rules Engine → Weighted Scoring Engine (14 dimensions)
- Add dimension weight table with sigmoid confidence calibration
- Update LLM classifier fallback trigger (confidence < 0.70)
- Update test results, architecture tree, programmatic usage example
- Add Phase 1 completion + Phase 2/3 to roadmap
Mismatch between openclaw.plugin.json (clawrouter) and src/index.ts
(claw-router) caused config validation to fail when the bot's agent
added the runtime ID to plugins.entries. Also bumps USER_AGENT and
descriptions to v0.2.1 / 78% savings.
1bcMax and others added 25 commits February 9, 2026 13:23
The reinstall script was removing clawrouter from plugins.allow but never
adding it back after reinstall. This caused the plugin to not load.

Fixes issue where 'openclaw gateway run --verbose' shows zero ClawRouter logs.
OpenClaw sends 'developer' role which BlockRun API rejects.
Now normalizes to standard OpenAI roles before forwarding.
- Create docs/troubleshooting.md with all troubleshooting content
- Create docs/features.md with advanced features (agentic, tool detection, etc)
- Expand docs/configuration.md with wallet backup/recovery and programmatic usage
- Fix 14-dimension to 15-dimension scoring, add agentic task dimension
- Slim README with brief summaries linking to detailed docs
- Update nav links to include new docs
BREAKING CHANGE: ClawRouter no longer forces blockrun/auto as default model.
Users must explicitly opt-in via /model blockrun/auto.

Fixes:
- Remove forced default model override in config and at runtime
- Reduce model allowlist injection from 50+ to 12 key aliases
- Add uninstall.sh script for clean removal
- Update README Quick Start to reflect opt-in model
- Include scripts directory in npm package

This addresses feedback about invasive install behavior that broke
existing OpenClaw setups using other providers.
Keep smart routing (blockrun/auto) as default model per user preference.
Still reduces allowlist pollution from 50+ to 12 key aliases.
Uninstall script and scripts in npm package retained.
UX Improvements:
- Non-blocking startup: balance check moved after proxy ready
- Universal free fallback: all requests fallback to free model when wallet empty
- Pricing shown upfront in startup messages
- Better error messages with actionable options
- Proxy health check before commands work (async register)

Issue BlockRunAI#12 Fix:
- Add normalizeMessagesForThinking() for thinking-enabled requests
- Ensures assistant messages with tool_calls have reasoning_content
- Fixes '400 thinking is enabled but reasoning_content missing' error
When a model returns 429 rate limit:
- Mark it as rate-limited for 60 second cooldown
- Future requests put rate-limited models at end of fallback chain
- Prevents double-charging users when first model is rate-limited
Critical fixes to prevent HTTP proxy from dying after 4+ hours:

Phase 1 (CRITICAL):
- Add backpressure handling with safeWrite() helper
- Add request/response finished() wrappers for guaranteed cleanup
- Add canWrite() pre-write verification to prevent EPIPE
- Guard heartbeat interval writes against closed sockets

Phase 2 (HIGH):
- Add socket timeout handlers (5min) to prevent FD exhaustion
- Add server.close() timeout (4s) for clean OpenClaw restarts
- Track socket lifecycle (timeout, end, error, close events)

Test Infrastructure:
- Add comprehensive resilience test suite
- test/resilience-errors.ts - EPIPE, ECONNRESET, timeout, malformed HTTP
- test/resilience-stability.ts - Long-running load simulation
- test/resilience-lifecycle.ts - OpenClaw SIGUSR1 restart tests
- Move existing test files to test/ directory for organization

Test Results (100% pass):
- Error simulation: 5/5 passed
- Lifecycle tests: 4/5 passed (port release expected failure)
- Stability: 5min load test, 0% error rate, stable memory
…pped

The 'openclaw plugins install' command on line 57 blocks, causing steps
5-7 (auth profile, allow list, apiKey setup) to never execute. This left
users with incomplete configuration.

Fix: Move config setup (steps 4-6) before the install (step 7), so the
plugin starts with correct configuration already in place.

Resolves issue where plugins.allow was empty and apiKey was missing.
The previous fix moved config setup before install, but left the allow list
step before the install. This caused validation to fail because:
- Step 5 added 'clawrouter' to plugins.allow
- Step 7 ran 'openclaw plugins install'
- Install validation saw 'clawrouter' in allow list but no plugin files
- Validation failed with exit 1

New order:
1-5: Setup (remove, clean, kill, cache, auth, apiKey)
6: Install plugin (plugin files now exist)
7: Add to allow list (after install, so validation passes)

Note: Install still blocks on proxy start - this is a separate issue.
- Add localBackends config option in plugin settings
- Create local-backend.ts with Claude CLI handler
- Integrate local backend check in proxy before x402 flow
- Update README with configuration documentation

When enabled, Anthropic models (claude, sonnet, opus, haiku) are routed
to the local Claude CLI instead of x402 payments. Useful for users with
Claude Pro/Max subscriptions who want to use their existing auth.

Other models (GPT, Gemini, Grok, etc.) continue through ClawRouter.

Closes BlockRunAI#14
@gigi-trifle
gigi-trifle force-pushed the feature/claude-cli-integration branch from 1b1ebfe to 40e53b3 Compare February 10, 2026 13:08
gigi-trifle and others added 3 commits February 10, 2026 15:38
The plugin config schema was missing the localBackends property,
causing OpenClaw to reject the config key during validation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
OpenClaw's plugin loader does not await async register() functions,
and SIGUSR1 hot-reloads only call service stop()/start() without
re-calling register(). Move proxy startup into the service start()
callback so it survives hot-reloads.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Claude CLI doesn't support --max-tokens, causing it to exit
immediately with an error. The subsequent stdin.write() then threw
an unhandled EPIPE that crashed the entire gateway process.

- Remove --max-tokens arg (not a valid Claude CLI flag)
- Add stdin error handler to prevent EPIPE from crashing the gateway

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configure to allow local Claude CLI instance with Pro/Max subscription

4 participants