A high-performance Go 1.26 gateway that fronts the FreeBuff/Codebuff CLI wire protocol, providing full dual-protocol compatibility for both OpenAI and Anthropic API clients. It enables any AI agent harness (Claude Code CLI, Cline, Roo Code, Cursor, Aider, Windsurf, OpenCode, OMP, Vercel AI SDK, LiteLLM, LangChain) to interface seamlessly with FreeBuff upstream models in either Pooled or Bridge mode.
A Svelte 5 SPA dashboard is embedded via go:embed in internal/dashboard and served under /admin.
Official reference specifications and SDKs reside in reference/ (gitignored).
POST /v1/messages— Full streaming SSE and non-streaming responses.- Streaming events:
message_start(with estimatedinput_tokens),content_block_start(thinking/text/tool_use),content_block_delta(thinking_delta, text_delta, input_json_delta, signature_delta),content_block_stop,message_delta(withoutput_tokens),message_stop, and standardevent: pingkeepalive. - Automatic stripping of proxy-injected
end_turnpseudo-tools so Claude Code CLI never fails on undeclared tools. - Thinking blocks contain required
"signature": ""for strict TypeScript/Zod schema compliance. - Formatted Anthropic error envelopes:
{"type": "error", "error": {"type": "...", "message": "..."}}. - Version header support (
anthropic-version: 2023-06-01).
- Streaming events:
POST /v1/messages/count_tokens— Local deterministic token estimation usingo200k_baseBPE tokenizer.
POST /v1/chat/completions— Streaming SSE (chat.completion.chunk) and non-streaming (chat.completion).- Strict OpenAPI 3.1 schema compliance:
refusal: null,logprobs: null, and standardusageobject. - Streaming XML tool-call extraction (
<tool_call>,<codebuff_tool_call>,<function_call>,<|tool_call_start|>, fenced JSON) into nativetool_callsdeltas with sequential synthetic indices. - Legacy function normalization: auto-translates
functionsandfunction_callintotoolsandtool_choice. - Reasoning effort ladders (
minimal,low,medium,high,xhigh) and think tag extraction (<think>...</think>).
- Strict OpenAPI 3.1 schema compliance:
POST /v1/responses— OpenAI Responses API translation.GET /v1/models— Full catalog model list with live availability and tier annotations.GET /v1/models/{model...}— Single model retrieval endpoint supporting slash-delimited model names.POST /v1/embeddings— Embeddings proxy endpoint.
-
Pooled Mode (Default when
AUTH_TOKENSis set):- Proxy manages a fixed pool of upstream FreeBuff tokens.
- Handles admission coercion, rate limit backoffs, Pacific midnight resets, country blocks, and token rotation.
- Client authentication via optional
API_KEYS.
-
Bridge Mode (Active when
AUTH_TOKENSis unset/empty):- Zero pre-configured credentials required.
- Authenticates per-request FreeBuff tokens from
Authorization: Bearer <token>,x-api-key: <token>, oranthropic-api-key: <token>. - Dynamically leases, caches, and maintains upstream sessions and runs per client token.
cmd/freebuff-proxy— Entrypoint, CLI flag parsing (-doctor,-test-token,-version,-config,-setup).internal/config— Typed configuration loader,.env+ JSON precedence, hot-reloading viaatomic.Pointer.internal/registry— Model catalog synced from upstream; alias resolution and theServedModelsgate.internal/convert— Pure conversion logic:convert.go— Request normalization, parameter whitelisting, role rewriting (developer→system), legacy function normalization.accumulator.go— Non-streaming response assembler, XML tool call extractor,Finish()JSON builder.effort.go— Reasoning effort extraction, thinking budget scaling, think tag stripping.schemacache.go— Tool JSON schema normalization ($ref inlining, schema caching) andend_turntool injection.sse.go— SSE frame encoder/decoder, chunk sanitization, and end-turn stripping.streamxml.go— Incremental XML stream parser.
internal/server— HTTP router and handlers:server.go— Router initialization, middleware wiring, lifecycle management.chat.go— OpenAI chat completions handler,relayStream,relayJSON, and token lease acquisition.anthropic.go— Anthropic/v1/messagesand/v1/messages/count_tokenshandlers, request translation,relayAnthropicStream,relayAnthropicJSON.responses.go— OpenAI Responses API handler and event streamer.models.go—GET /v1/modelsandGET /v1/models/{model...}handlers.streamxml.go— Streaming XML tool call extraction relays for chat and Anthropic.errors.go— Protocol-aware error formatting (OpenAI vs Anthropic) and PRD error mapping.middleware.go— Auth validation (Authorization,x-api-key,anthropic-api-key), CORS, access logging.admin*.go— Dashboard authentication, CSRF, config editor, token management API.
internal/pool— Token lifecycle, admission, bridge caching, cooldowns, quota windows, and spend tracking (acquire.go,bridge.go,cooldown.go,quota.go,spend.go,unfit.go).internal/session— Upstream session manager and persistence.internal/upstream— FreeBuff wire client: session admission, chat relay, rate limit parser, stealth profiles.internal/tokenestimate— Localo200k_baseBPE tokenizer for Anthropic token counting and streaming input token estimation.internal/runs— Run lifecycle: START/FINISH, step counting, drain queues.internal/reasoningcache— Multi-turn reasoning cache for assistant reasoning restoration.internal/ratelimit— Per-IP token bucket rate limiter.internal/stealth— TLS fingerprinting (utls) and header sanitization.internal/telemetry— Prometheus/metricsinstrumentation.internal/logring— In-memory ring buffer for dashboard log streaming.internal/dashboard— Embedded single-page application (assets_embed.go/assets_stub.go).
- Hermetic Test Suite: Always unset
AUTH_TOKENSandADMIN_TOKENwhen running tests to avoid polluting bridge-mode test environments:env -u AUTH_TOKENS -u ADMIN_TOKEN go test ./... - Anti-Ban Contract:
- Upstream session POST sends
x-freebuff-modelandx-freebuff-instance-id. - Upstream chat POST carries NO model header.
- Pinned
ai-sdkUser-Agent on all chat requests. - Honest
FINISHon run termination.
- Upstream session POST sends
- Tool Stripping Parity:
- The proxy injects
end_turnto satisfy upstream schema requirements. end_turnMUST be stripped before emitting to downstream clients (both OpenAI and Anthropic relays).
- The proxy injects
- Sequential SSE Content Blocks:
- Anthropic streaming protocol requires strictly sequential block lifecycles (
content_block_start→content_block_delta→content_block_stop). Never interleave unclosed blocks.
- Anthropic streaming protocol requires strictly sequential block lifecycles (
# Build binary
go build -o freebuff-proxy.exe ./cmd/freebuff-proxy
# Run full hermetic test suite
env -u AUTH_TOKENS -u ADMIN_TOKEN go test ./...
# Check code formatting
gofmt -l cmd internal
# Format code
gofmt -w cmd internal
# Run CLI diagnostics
./freebuff-proxy.exe -doctor
# Probe configured token
./freebuff-proxy.exe -test-tokenSingle unified trunk on main. All features, CLI utilities, and embedded dashboard components live in this repository.
- Feature branches:
feat/<name>,fix/<name>, orrefactor/<name>. - Pull requests target
main. - Never
git push --forceonmain. - The proxy can be compiled with or without the embedded dashboard via Go build tags (
-tags dashboard) and configured at runtime viaDASHBOARD_ENABLED.
- This repository is public. Only public-safe, secret-free content may ever be committed.
- Dev-only and reverse-engineering artifacts stay gitignored (
reference/,devdocs/,.env*,config*.json,*.session-state.json). - Commits follow Conventional Commits (
feat(...),fix(...),refactor(...),docs(...),test(...)).