Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AnyRewriter

Fastify rewriter/proxy for Claude-compatible requests through AnyRouter, plus the read-only operations dashboard source that was paired with it in production.

The 2026-08-20 source snapshot was taken before the retired deployment was removed. It intentionally excludes .env, credentials, databases, control state, request dumps, event streams, access logs, generated state, backups, and container/runtime evidence.

What it does

  • Proxies Claude API paths (/v1/*, /anthropic/*) to an upstream gateway.
  • Rewrites unsupported model names to a configured target model.
  • Preserves supported Claude model families, including haiku, opus, and fable.
  • Adds required anthropic-beta flags when clients omit them.
  • Repairs malformed historical tool_result blocks caused by cross-protocol history pollution.
  • Removes empty-signature thinking blocks that can trigger upstream validation failures.
  • Keeps historical-thinking pruning available as an emergency option, but defaults it off.
  • Learns 524 / slow-520 stuck-body fingerprints and strips message-level thinking only on the client's retry.
  • Uses deterministic hash-daily / hash-hourly session IDs to recover from session-level bans.
  • Passes through rate-limit and pool-level failures instead of wasting retries.
  • Aborts upstream work when the downstream client disconnects mid-stream.

Requirements

  • Node.js 22+
  • npm packages:
    • fastify
    • undici

The committed package.json and package-lock.json reproduce the rewriter dependencies.

Run

npm ci
UPSTREAM_URL=https://anyrouter.top \
TARGET_MODEL=claude-opus-4-8 \
PORT=8787 \
node rewriter.js

dashboard/server.js and dashboard/public/index.html preserve the dashboard source. The dashboard expects explicitly supplied, local read-only data paths; no production data is included in this repository.

Environment

Variable Default Purpose
UPSTREAM_URL http://new-api:3000 Upstream API base URL.
TARGET_MODEL claude-opus-4-8 Fallback model for unsupported model names.
PORT 8787 Listen port.
AUTO_PRUNE_HISTORY_THINKING false Optional emergency historical-thinking pruning.
THINKING_RESCUE_AFTER_524 true Enable learn-first 524/slow-520 thinking rescue.
UPSTREAM_HEADERS_TIMEOUT_MS 320000 Header wait timeout before aborting a stuck upstream fetch.

Notes

rewriter.js is designed as an operational compatibility shim. It should avoid changing user semantic settings such as output_config.effort; it only changes request structure when needed for upstream compatibility or recovery.

About

No description, website, or topics provided.

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages